Discussion:
VBA, accepting input from text box
(too old to reply)
Sam
2007-01-03 02:16:20 UTC
Permalink
I have a macro that will create a new configuration for all documents
in a selected folder but the only way I can get it to work is to "hard
code" the configuration name by putting the configuration name directly
into the code. I would like to change this so that the user can input
the new configuration name into a text box and then assign that value
to a variable that is in turn referenced in the code so that the user
can define the configuration name at run time rather than having to
edit the code to enter the configuration name. I can create the form,
text box and label my problem appears to be with the code.

Does anyone have a simpe macro they can share that includes an example
of how to accept input from a text box?

Thanks

Sam
Tin Man
2007-01-03 10:50:08 UTC
Permalink
Here's something that should get you going...

Make a new macro with a Userform (called frm1) that contains a Textbox
(called txt1) and a Command Button (called cmd1). Paste the below code
into the Userform code.

Ken


Private Sub cmd1_Click()

Dim strConfigName() As String
Dim intArrayCount As Integer
Dim intCount As Integer

intArrayCount = 1
ReDim strConfigName(intArrayCount)
For intCount = 1 To Len(frm1.txt1.Text)

'Current character ASCII code if interested
'You'll need this to check for invalid characters (if there are
any?),
' or maybe this could be better used in the txt1_KeyDown Event
to
' disallow the invalid character from even being entered (if
needed).
Debug.Print Asc(Mid$(frm1.txt1.Text, intCount, 1))

If Asc(Mid$(frm1.txt1.Text, intCount, 1)) = 13 Then '13 is the
Enter Key
'Ignore this character and increment configuration name
array if needed
If strConfigName(intArrayCount) <> "" Then
intArrayCount = intArrayCount + 1
ReDim Preserve strConfigName(intArrayCount)
End If
ElseIf Asc(Mid$(frm1.txt1.Text, intCount, 1)) = 10 Then '10 is
the Line Feed
'Ignore this character and increment configuration name
array if needed
If strConfigName(intArrayCount) <> "" Then
intArrayCount = intArrayCount + 1
ReDim Preserve strConfigName(intArrayCount)
End If
ElseIf Mid$(frm1.txt1.Text, intCount, 1) <> "" Then
'Add this character to the current configuration name in
the array
strConfigName(intArrayCount) = strConfigName(intArrayCount)
& Mid$(frm1.txt1.Text, intCount, 1)
End If
Next intCount

'Discard last configuration name in the array if it is blank
If strConfigName(intArrayCount) = "" Then
intArrayCount = intArrayCount - 1
ReDim Preserve strConfigName(intArrayCount)
End If

'Display each configuration name entered
For intCount = 1 To UBound(strConfigName())
MsgBox "Configuration Entered #" & intCount & ": " &
strConfigName(intCount)
Next intCount

End Sub

Private Sub UserForm_Initialize()
frm1.txt1.EnterKeyBehavior = True
frm1.txt1.MultiLine = True
End Sub
fcsuper
2007-01-03 16:59:18 UTC
Permalink
Sam,

There's lots of examples of how to do this online. I have several
macros using different methods on my own site: http://sw.fcsuper.com.
Under Downloads, check out Rigid Custom Properties, Text to Clipboard,
Save as PDF (all under All Document Types section), and Add Rev to Rev
Block (under drawings section).

Matt
Post by Sam
I have a macro that will create a new configuration for all documents
in a selected folder but the only way I can get it to work is to "hard
code" the configuration name by putting the configuration name directly
into the code. I would like to change this so that the user can input
the new configuration name into a text box and then assign that value
to a variable that is in turn referenced in the code so that the user
can define the configuration name at run time rather than having to
edit the code to enter the configuration name. I can create the form,
text box and label my problem appears to be with the code.
Does anyone have a simpe macro they can share that includes an example
of how to accept input from a text box?
Thanks
Sam
Sam
2007-01-03 18:01:18 UTC
Permalink
Thanks for the help guys, unfortunately your examples are too
complicated for me to figure out. All I want to figure out is how to
assign a value from a text box to a variable and then reference that
variable in the code so that a configuration is created with the
configuration name being what the user input into the text box. I have
the form created, and I can see that the value in the text box is being
assigned to the variable but for some reason the line of code to create
the config is not working. The line of code to add the config is below,
ConfigName is the variable and its value is assigned from the text box
but I cannot figure out why the config is not being created. If I were
to enclose ConfigName in parenthesis then a config named ConfigName
would be created so the code appears to be correct in that application
but I do not want to "hard code" the configuration name into the
program, I want it to pick up the name from the text box.


ConfigMgmr.AddConfiguration ConfigName, "", "", 0, "", ""
f***@gmail.com
2007-01-03 18:44:07 UTC
Permalink
Hi Sam, I'm trying to learn VBA with Solidworks myself. I was wondering
if you could post/send me the macro of what you have? I agree that most
things I find online or way too complicated, and I need something
simple to start with to learn from. As for the inputbox command, try:

variable = InputBox("Please enter some String")

Then use that variable where needed. I hope that helps. (If this isn't
working sorry, I'm new to this myself)
fcsuper
2007-01-04 19:14:38 UTC
Permalink
For simplicity, I also recommend Inputbox. Using forms can be
complicated for the novice, especially when getting into listboxes.
Some on the examples I sited before have good examples on how to use
Inputbox. It's pretty much just two lines of code gets you what you
want:

Dim ClipBoardText As String

ClipBoardText = InputBox("Enter text you want put on clipboard.",
"Clipboard Test", "Enter your text here")

The first section "Enter text you want..." is the text used to provide
instructions, the second "Clipboard Test" is the name of the inputbox
shown in the title bar, and the third "Enter your text..." is the
default text in the input field.


Matt
Post by f***@gmail.com
Hi Sam, I'm trying to learn VBA with Solidworks myself. I was wondering
if you could post/send me the macro of what you have? I agree that most
things I find online or way too complicated, and I need something
variable = InputBox("Please enter some String")
Then use that variable where needed. I hope that helps. (If this isn't
working sorry, I'm new to this myself)
Tin Man
2007-01-04 11:20:25 UTC
Permalink
Post by Sam
All I want to figure out is how to
assign a value from a text box to a variable and then reference that
variable in the code
ConfigName = Userform1.Textbox1.Text
Post by Sam
ConfigMgmr.AddConfiguration ConfigName, "", "", 0, "", ""
Before the above line in your code, you can put this line:
Msgbox "The value of ConfigName is: " & ConfigName
This will pop up another dialog box to let you know the value of your
variable at that time. Useful only during the debugging stage. From the
sounds of it, the ConfigName variable is a blank string.

You could also try this:
ConfigMgmr.AddConfiguration Userform1.Textbox1.Text, "", "", 0, "", ""

You'd probably get a better response if you actually posted the code.

Ken
That70sTick
2007-01-04 14:58:07 UTC
Permalink
I recommend using a form with a ListBox so that the user can select the
configuration from a list rather than typing (mis-typing!) the config
name.

The "Copy Custom Info" macro on my website uses configs in a drop-down
list. Feel free to examine the code.
<http://www.esoxrepublic.com/freeware/>

If you are serious about macro programming, spend some time learning
VB6 or VBA first to get a grip on object-oriented programming and forms.
Sam
2007-01-04 20:24:08 UTC
Permalink
Mr. Tick, :)

I had taken VB several years ago and I would really like to be able to
use it more unfortunately my job does not allow time for it but only a
few times a year maybe. So even when I do learn something new by the
time I come back to it I have already forgotten what I had learned.

Thanks for the responses, maybe I will be able to spend some time this
weekend to decipher the example code that every one referenced.

Thanks again...

Sam

Loading...