Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a variable from a procedure to a User Form

Status
Not open for further replies.

AnnaWnz

MIS
Dec 10, 2002
22
0
0
NZ
Hi

Just wondering how you go about passing a variable that has a value set within a procedure in the objects part of the project, to a userform.

I can pass variables from the userform to the procedure okay, but not the other way around.

I have tried declaring the variabe as Public (had to do this in both the form and in the general declarations above the procedure, otherwise I got a Variable Not Defined error), and making all my procedures public, but this doesn't seem to work.

Procedure in the Objects area:

Public Sub InvalidNum(Length, ContainerNum)
If (Length < 10) Or (Length > 11) Then
Rem the Batch ID entered is not long enough to be a valid one.
Rem User needs to correct before going further
frmInvalidNum.Show
If frmInvalidNum.Answer2 = 2 Then
Rem if the the user clicks Add this Batch ID, then continue with adding the batch
BatchID = ContainerNum
Exit Sub
ElseIf frmInvalidNum.Answer2 = 1 Then
Rem if the user clicks Re-enter, then go back to the Batch ID field to correct entry
Call Retry
End If
End If
End Sub


User Form:

Option Explicit
Dim Batch_ID As String
Public Answer2 As Integer

Private Sub cmdAddBatch_Click()
Answer2 = 2
Hide
End Sub

Private Sub cmdReenter_Click()
Answer2 = 1
Hide
End Sub

Private Sub UserForm_Activate()
frmInvalidNum.lblBatchID.Caption = ContainerNum
End Sub

I want to pass the value of the variable ContainerNum from the procdure to the form.

Cheers
Anna
 
You didn't specify what application you are using, and you reference to an &quot;Objects&quot; area is unfamiliar to me. But, since this is the VBA forum (and not VB), just on the chance that you are using Access, for example, here is one way:

In a separate module:
Code:
Public datum As String
Public Sub setdatum()
  datum = &quot;Merry Christmas&quot;
End Sub

In the form code (contains Command button and text field):
Code:
Private Sub Command1_Click()
  setdatum
  Text1.Value = datum
End Sub

If you are doing something else, please be more specific.



 
If you declare &quot;Public&quot; variable in a standard module, you will be able to set and read its value while displaying userforms.
 
alternatively, you can try something like this.

' in your form's declaration section:
Public ContainerNum As String

Private Sub UserForm_Activate()
lblBatchID.Caption = ContainerNum
End Sub

' in your procedure's code, just before you show the form, add this.

Public Sub InvalidNum(Length, ContainerNum)
...
frmInvalidNum.ContainerNum = &quot;9876543210&quot;
frmInvalidNum.Show
...
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top