PDA

View Full Version : VB6 Homework help – passing arguments from forms to modules and back


naosuke
04-23-2005, 09:35 PM
I am trying to clean up my code. My assignment was to write code to verify data in text fields e.g. txtRegNo should not be blank and it should be numeric. All of the text fields appear in multiple forms. What I am trying to do is add a function that can be accessed by all three forms so that I won’t have the same code in three different places. I was told that generally you would put that in a module, but once I did my code stopped working. I am new to programming so any help would be greatly appreciated.

oracleguy
04-24-2005, 07:45 PM
Well, maybe you could post your code so we can see exactly what your doing. Then we might be able to see what's wrong.

naosuke
04-24-2005, 07:57 PM
This is the code from Form 1

Private Sub cmdSaveRecord_Click()
Dim IsValidData As Boolean
IsValidData = True
If Me.txtRegNo = "" Then
MsgBox "Registration number should not be blank"
Me.txtRegNo.SetFocus
IsValidData = False
Exit Sub
ElseIf Not IsNumeric(Me.txtRegNo) Then
MsgBox "Registration number must be numeric"
Me.txtRegNo.SetFocus
IsValidData = False
Exit Sub
End If
If Me.txtFirstName = "" Then
MsgBox "First name should not be blank"
Me.txtFirstName.SetFocus
IsValidData = False
Exit Sub
End If
If Me.txtLastName = "" Then
MsgBox "Last name should not be blank"
Me.txtLastName.SetFocus
IsValidData = False
Exit Sub
End If
Dim strDate As String
strDate = Me.cboMonth.ListIndex + 1
strDate = strDate + "/" + Me.cboDay.Text
strDate = strDate + "/" + Me.cboYear.Text
If Not IsDate(strDate) Then
MsgBox "Invalid Date"
IsValidData = False
Exit Sub
End If
End Sub

As you can see this overlaps a large amount with the relevant code from form 2

Private Sub cmdSaveRecord_Click()
Dim IsValidData As Boolean
IsValidData = True
If Me.txtRegNo = "" Then
MsgBox "Registration number should not be blank"
IsValidData = False
Me.txtRegNo.SetFocus
Exit Sub
ElseIf Not IsNumeric(Me.txtRegNo) Then
MsgBox "Registration number must be numeric"
IsValidData = False
Me.txtRegNo.SetFocus
Exit Sub
End If
If Me.txtAddress = "" Then
MsgBox "Address should not be blank"
IsValidData = False
Me.txtAddress.SetFocus
Exit Sub
End If
If Me.txtphone <> "" Then
If Not IsNumeric(Me.txtphone) Then
MsgBox "Telephone number must be numeric"
IsValidData = False
Me.txtphone.SetFocus
Exit Sub
End If
End If
If Me.txtCountry = "" Then
MsgBox "Country should not be blank"
IsValidData = False
Me.txtCountry.SetFocus
Exit Sub
End If
If Me.txtZipCode = "" Then
MsgBox "Zip Code should not be blank"
IsValidData = False
Me.txtZipCode.SetFocus
Exit Sub
ElseIf Not IsNumeric(Me.txtZipCode) Then
MsgBox "Zip Code must be numeric"
IsValidData = False
Me.txtZipCode.SetFocus
Exit Sub
End If
If Me.optMale = False And Me.optFemale = False Then
MsgBox "You Must Choose a Gender"
IsValidData = False
Exit Sub
End If
Dim strDate As String
strDate = Me.cboMonth.ListIndex + 1
strDate = strDate + "/" + Me.cboDay.Text
strDate = strDate + "/" + Me.cboYear.Text
If Not IsDate(strDate) Then
MsgBox "Invalid Date"
IsValidData = False
Exit Sub
End If
End Sub


What I'm trying to do is create a module that will handle at least the overlap, but ideally, it would handle both. The problem is that I do not know how to pass information from Me.txtRegNo to a module or get the module to return the right information.

naosuke
04-24-2005, 08:37 PM
I had the idea of declaring the text in the text boxes as strings but it isn't working.
This is my current code from form 1 followed by the code from the module ValidData
cmdResetAll_Click()
Me.txtRegNo.Text = ""
Me.txtFirstName.Text = ""
Me.txtLastName.Text = ""
Me.cboMonth.Clear
Dim X As Integer
For X = 1 To 12
Me.cboMonth.AddItem MonthName(X)
Next
Me.cboMonth.ListIndex = 0
Me.cboDay.Clear
For X = 1 To 31
Me.cboDay.AddItem X
Next
Me.cboDay.ListIndex = 0
Me.cboYear.Clear
For X = Year(Date) To (Year(Date) - 15) Step -1
Me.cboYear.AddItem X
Next
Me.cboYear.ListIndex = 0
Me.txtRegNo.SetFocus
End Sub
Public Function IsValidData() As Boolean
If txtRegNo = "" Then
MsgBox "Registration number should not be blank"
IsValidData = False
Exit Function
ElseIf Not IsNumeric(Me.txtRegNo) Then
MsgBox "Registration number must be numeric"
IsValidData = False
Exit Function
End If
End Function

so far I've only coded the validation of txtRegNo. Are strings the way to go about it, or am I barking up the wrong tree? If strings are the way to fix my problem how should go about making things work?

shmoove
04-24-2005, 11:21 PM
I think you should probably be passing the value of the text fields as parameters to the functions in the module. Something like:

Public Function IsNumericAndNotEmpty(TheData as String, FieldName as String) As Boolean
If TheData = "" Then
MsgBox FieldName & " should not be blank"
IsValidData = False
Exit Function
ElseIf Not IsNumeric(TheData) Then
MsgBox FieldName & " must be numeric"
IsValidData = False
Exit Function
End If
End Function

Notice that I've also passed the name of the field to be used in the MsgBoxes so the function can be more reusable. The usage would look something like this:

Private Sub cmdSaveRecord_Click()
Dim IsValidData As Boolean
IsValidData = True
IsValidData = IsNumericAndNotEmpty(Me.txtRegNo,"Registration number")
' etc.
End Sub

You could make a couple of other validating functions (IsNotEmpty, IsValidData) and reuse them in all the forms.

shmoove