Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Storing and Retrieving Variables Question

I am trying to store a Variable that is acquired by a text box 'Input1'
from a form that I had created. On this form the button 'button1'
should call the variable stored in input1 and display it in a message
box...this is mostly me trying to learn how to use variables....I have
a nice book on VBA sitting next to me and I am somehow missing the
whole variable thing...here is an example of my code that doesn't
work....any tips would be much appreciated.


Public Sub box1_Change()
Dim Input1 As String
Input1 = box1.Value
End Sub
----------------------------------------
Sub button1_Click()
MsgBox Input1
End SubPublic Sub box1_Change()

For the record I haven't really nailed down the Public Sub, Private Sub
and Sub meanings so if that is also incorrect it wouldn't surprise
me...my book does go into that stuff.
Thank you,
Jason Self


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Storing and Retrieving Variables Question

Just move your Dim statement to outside of your Sub procedure. The
problem is when you declare a variable inside a sub procedure it only
is valid within that sub procedure, to have it active for the entire
Form/Module, move it to the top of your program as either a Dim or
Private for it to be valid through the Form/Module, or a Public to have
it be valid through all Forms and Modules (Project).


---
Message posted from http://www.ExcelForum.com/

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Storing and Retrieving Variables Question

Jason,

This does not work because Input1 is a local variable to the box1_Change
procedure. So when you try to reference it in the button1_Click procedure,
a new variable is created local to that procedure, and it has no value. If
you had used Option Explicit, which forces you to declare your variables, at
the head of your code, you would have got an error in the second procedure
that Input1 does not exits.

As it happens, you can reference the textbox directly from the button
procedure, like this


Private Sub button1_Click()
MsgBox box1.Value
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"JasonSelf " wrote in message
...
I am trying to store a Variable that is acquired by a text box 'Input1'
from a form that I had created. On this form the button 'button1'
should call the variable stored in input1 and display it in a message
box...this is mostly me trying to learn how to use variables....I have
a nice book on VBA sitting next to me and I am somehow missing the
whole variable thing...here is an example of my code that doesn't
work....any tips would be much appreciated.


Public Sub box1_Change()
Dim Input1 As String
Input1 = box1.Value
End Sub
----------------------------------------
Sub button1_Click()
MsgBox Input1
End SubPublic Sub box1_Change()

For the record I haven't really nailed down the Public Sub, Private Sub
and Sub meanings so if that is also incorrect it wouldn't surprise
me...my book does go into that stuff.
Thank you,
Jason Self


---
Message posted from http://www.ExcelForum.com/



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lookup Question with multiple dynamic variables Robin Excel Worksheet Functions 14 June 12th 08 09:40 PM
Lookup Question Based upon 2 Variables Lois Excel Worksheet Functions 2 May 23rd 08 11:37 PM
general question about variables integreat Excel Discussion (Misc queries) 2 May 20th 06 07:29 AM
Question about scoping variables TBA[_2_] Excel Programming 1 January 8th 04 01:47 AM
Storing Variables Todd Huttenstine[_3_] Excel Programming 5 November 27th 03 02:14 AM


All times are GMT +1. The time now is 05:30 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"