variables not keep their values

C

Craig

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
 
J

Jezebel

You would have saved yourself a heap of bother if you'd read Help or any of
a squillion web tutorials on variable scoping in VB/VBA.

As a quick fix, put 'Option Explicit' at the top of your form (and every
other) code module. Then you would have got compiler errors as soon as you
tried to run your form, which would also have alerted you to the problem.

In short, variables declared within a function (such as those you declare in
Initialize) are valid ONLY WITHIN THAT FUNCTION. If you want module-level
variables, you need to declare them at the start of the module, outside any
function.

Separately, your declarations are not doing what you intend --
Dim x, xval As Integer
Dim xbatch, xweek, xdow As String

The 'as' part applies only to the last variable in each case. As it stands,
you are declaring x, xbatch, and xweek as variants.

And it is more efficient to use Longs than Integers. Integers are provided
only for backward compatability.
 
C

Craig

Thanks. I'll look that up and give it a try. This is the first time I've
written any code since 1990 and the last thing I wrote was in Pascal. I do
have a degree in computer programing, but I've never written anything or even
looked at VBA until now, and the only reason I'm doing it now is I'm the most
experienced in my department....lol
 
J

Jezebel

Pascal had scoping and declaration rules, too, if my recollections of
antedeluvian programming are still valid ...
 
D

Doug Robbins - Word MVP

Please do not post the same question separately to multiple newsgroups. I
now see that I have spent time answering your question in the
microsoft.public.word.vba.userforms newsgroup only to find the same question
here has already been answered.

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top