Results 1 to 10 of 10

Thread: Question regarding validating variables in text...

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    5

    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.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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)

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    5
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    5
    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

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    5
    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

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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).

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    5
    Omg, it worked. You are hella smooth. With 5 minutes of your time, you corrected a weeks worth of headaches. Thanks so much

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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
  •  



Click Here to Expand Forum to Full Width