Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

variables not keep their values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Craig - 20 Dec 2006 04:27 GMT
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
Jezebel - 20 Dec 2006 05:46 GMT
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.

>I have created a userform containing 1 textbox, 2 comboboxes, and 1
> commandbutton.
[quoted text clipped - 64 lines]
>
> End Sub
Craig - 20 Dec 2006 06:05 GMT
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

> 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.
[quoted text clipped - 87 lines]
> >
> > End Sub
Jezebel - 20 Dec 2006 06:49 GMT
Pascal had scoping and declaration rules, too, if my recollections of
antedeluvian programming are still valid ...

> 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
[quoted text clipped - 105 lines]
>> >
>> > End Sub
Doug Robbins - Word MVP - 20 Dec 2006 08:46 GMT
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.

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

> 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
[quoted text clipped - 105 lines]
>> >
>> > End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.