I have created a userform containing 1 textbox, 2 comboboxes, and 1
commandbutton.
My problem is this, when data is entered in the textbox or comboboxes it is
put into their variables but when I click on the commandbutton the variables
have no data in them anymore.
I have a test msgbox that displays the variables and the data is there, but
the test msgbox in the command button shows the variables as empty.
I have included the entire code below:
Private Sub TextBox1_Change()
xbatch = TextBox1
xval = 1
End Sub
Private Sub ComboBox1_Change()
xweek = ComboBox1
xval = 2
MsgBox (xweek & "-" & xval)
End Sub
Private Sub ComboBox2_Change()
xdow = ComboBox2
xval = 3
End Sub
Private Sub CommandButton1_Click()
' If xval = 3 Then
MsgBox (xbatch & ", " & xweek & ", " & xdow)
' Summary_of_Orders_Report xbatch, xweek, xdow
' End If
' MsgBox ("You must enter a value in all the fields" & vbCrLf & "xval = "
& xval)
End Sub
Private Sub userform_initialize()
'Set up the public variables the macro will use
Dim x, xval As Integer
Dim xbatch, xweek, xdow As String
'Initialize the week and day combo boxes by adding the correct values to
them
x = 1
For x = 1 To 52 Step 1 'The week number
ComboBox1.AddItem (x)
Next x
ComboBox2.AddItem ("Monday")
ComboBox2.AddItem ("Tuesday")
ComboBox2.AddItem ("Wednesday")
ComboBox2.AddItem ("Thursday")
ComboBox2.AddItem ("Friday")
End Sub
Doug Robbins - Word MVP - 20 Dec 2006 07:29 GMT
In the CommandButton1_Click routine, just refer directly to the .value
attribute of the other controls.
The reason however that your code is failing is that you have not declared
the variables as public and hence they are only valid in the individual
routines.
If you have Option Explicit as the first line of code, your routines would
not even compile as they are.

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
>I have created a userform containing 1 textbox, 2 comboboxes, and 1
> commandbutton.
[quoted text clipped - 64 lines]
>
> End Sub
Craig - 21 Dec 2006 03:41 GMT
Thanks Doug. I was tyring to declare my variables as public, but I was doing
it inside the sub. Jezzebel told me I had to move the public statement to
the top before I add any subs or functions and this worked just great.
> In the CommandButton1_Click routine, just refer directly to the .value
> attribute of the other controls.
[quoted text clipped - 74 lines]
> >
> > End Sub