|
-
Jul 21st, 2004, 07:03 AM
#1
Thread Starter
New Member
Question regarding validating variables in text...
Have a little problem here...what I am trying to do is a simple program that requires me to validate user info. I have a text file "Registered_Voters.txt" that I am trying to validate information from text boxes from. I have 3 text fields (txtFirstName, txtLastName, txtSSN) which I placed as a variant. So far my code is....
Dim varVoter As Variant
varVoter = txtFirstname.Text + txtLastName.Text + txtSSN.Text
The variable works. Now the problem....
I have a commandbutton that I'd like to search the text "Registered_Voters.txt" for the listed variant, and if the variant exists I want to continue to my next form, however, if it doesn't exist I will display a message box. Any help here would be appreciated, I've tried several loops to no avail. Thanks.
-
Jul 21st, 2004, 07:19 AM
#2
What code have you got for that part so far?
(it's much easier to modify what you have than to start again and then modify it)
-
Jul 21st, 2004, 07:24 AM
#3
You could either iterate through the text file line per line, concatenating on the fly and comparing or... you could also store the info in its original format (the original line with comma delimeters and vbCrLf at the end) aside from varVoter format. That way you could do an instring function search of the text stream.
-
Jul 21st, 2004, 07:28 AM
#4
Thread Starter
New Member
Private Sub cmdContinue1_Click()
Dim varVoter As Variant
varVoter = txtFirstName.Text + "_" + txtLastName.Text + "_" + txtSSN.Text
Dim i As Integer
i = 0
If txtFirstName.Text = "" Or txtLastName.Text = "" Or txtSSN.Text = "" Then
MsgBox "All fields must be entered."
Else
'Validate they are a registered voter
Open "H:\Voterinfo\Reg_Voters.txt" For Input As #4
Do While varVoter = False And Not EOF(4)
Loop
i = 1
Close #4
If i = 0 Then
MsgBox "You are not an eligable a voter."
frmMainMenu.Show
Unload Me
End If
For some reason, the code goes through ok, however, if I type in names etc that aren't contained in the text file I want to authenticate from, it still transitions to my next form instead of going through the "If i = 0" code.
-
Jul 21st, 2004, 07:32 AM
#5
Thread Starter
New Member
Was actually a screw up on my part, code so far is...
Private Sub cmdContinue1_Click()
Dim varVoter As Variant, i As Integer, g As Integer
varVoter = txtFirstName.Text + "_" + txtLastName.Text + "_" + txtSSN.Text
i = 0
g = 0
If txtFirstName.Text = "" Or txtLastName.Text = "" Or txtSSN.Text = "" Then
MsgBox "All fields must be entered."
Else
'Validate they are a registered voter
Open "H:\Voterinfo\Reg_Voters.txt" For Input As #4
Do While varVoter = False And Not EOF(4)
Loop
i = 1
Close #4
End If
If i = 0 Then
MsgBox "You are not an eligable a voter."
frmMainMenu.Show
Unload Me
End If
If i = 1 Then
Open "H:\Voterinfo\Results.txt" For Append As #8
Print #8, varVoter
Close #8
frmCandSelect.Show
Unload Me
End If
End If
-
Jul 21st, 2004, 07:37 AM
#6
Ahhh okie, how is the info stored in the text file? I'm assuming comma delimeted one voter per line. I still think InStr is easiest since yo can do an non case sensitive search.
-
Jul 21st, 2004, 07:43 AM
#7
Thread Starter
New Member
The info is stored as per the variant.
Variant is set to Firstname + "_" Lastname + "_" + SSN
So, when the variant actually displays it in the text file it would be:
Firstname_Lastname_SSN
Firstname1_Lastname_SSN
Willing to try anything at this point...the coffee kept me motivated towards self discovery for a while, the caffiene has long since left the system though. If you could, not quite sure what the code your saying to use would actually look like, an example would be helpful
-
Jul 21st, 2004, 07:56 AM
#8
You could do the search as is...
Open the text file
Load the stream into a string variable
If InStr(1, strStream, varVoter, vbTextCompare) > 0 Then Found
The problem would be with spaces since you searched for the concatenated format. Hmmmm... an array would be easier but it would be inefficient to load up an array only for a search (the text file is not loaded into an array for the duration of the program run).
-
Jul 21st, 2004, 08:08 AM
#9
Thread Starter
New Member
Omg, it worked. You are hella smooth. With 5 minutes of your time, you corrected a weeks worth of headaches. Thanks so much
-
Jul 22nd, 2004, 11:32 AM
#10
Glad to be of help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|