Results 1 to 12 of 12

Thread: [RESOLVED] how to stop it from writing the same details

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    81

    Resolved [RESOLVED] how to stop it from writing the same details

    The most basic registration form , if for example I run this and enter

    username1 and password1
    then do that again it will display in txt file as

    "username1","password1"
    "username1","password1"

    Is there an If statement I could use?, so if the username exists then exit sub :?


    Code:
    Dim savefile As Long
    savefile = FreeFile()
    Open "D:\a.txt" For Append As #savefile
    Write #savefile, (Text1.Text); (Text2.Text)
    Close #savefile

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: how to stop it from writing the same details

    It gets written twice because you are Appending, use Output instead.

    Open "D:\a.txt" For Output As #savefile

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    81

    Re: how to stop it from writing the same details

    Quote Originally Posted by Edgemeal
    It gets written twice because you are Appending, use Output instead.

    Open "D:\a.txt" For Output As #savefile
    when i use output it only writes 1 line, so cant have multiple logins

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how to stop it from writing the same details

    Then you really have no choice but to search the file to see if it already exists or not.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to stop it from writing the same details

    And you would probably want to check to see if the username and password you are about to write already exists in the file, or it would get duplicated.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    81

    Re: how to stop it from writing the same details

    Quote Originally Posted by Hack
    And you would probably want to check to see if the username and password you are about to write already exists in the file, or it would get duplicated.
    manualy or using vbcode?

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to stop it from writing the same details

    You are writing a program. There is no point in doing anything manually.

  8. #8
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: how to stop it from writing the same details


  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    81

    Re: how to stop it from writing the same details

    any example code :P?

  10. #10
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: how to stop it from writing the same details

    I have a login for my application, and they way I do it is I store all the usernames and passwords in a txt file, and at the start of my program, I load all the usernames into a string array, and all the passwords in another string array. That way anytime I need to do something with the usernames and passwords they're all right there to search through.

    Code:
       
        strBats = App.Path & "\bats\"
        intFileNum = FreeFile
        intIndex = 0
            
        Open strBats & "users.sun" For Input As intFileNum
        Do While EOF(intFileNum) = False
            ReDim Preserve strUser(intIndex), strPassword(intIndex)
            Input #intFileNum, strUser(intIndex), strPassword(intIndex)
            intIndex = intIndex + 1
        Loop
        Close #intFileNum
    Then when making changes (ie adding) to the users, you need only to make the change to the string arrays, then open the file for OUTPUT and write the arrays back in:

    Code:
                                Open strBats & "users.sun" For Output As intFileNum
                                For intIndex2 = LBound(strUser()) To UBound(strUser())
                                    Write #intFileNum, strUser(intIndex2), strPassword(intIndex2)
                                Next intIndex2
                                Close #intFileNum
    The only issue with doing it this way is to work with the username and passwords together, the indexes of the two arrays must stay the same. The username stored in the 4th index corresponds to the password stored in the 4th index. Gotta be careful not to change them in your code.

    Hope I could help!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    81

    Re: how to stop it from writing the same details

    I wonder why this does not work?, it was code that I used for login form,

    Code:
    Dim uname As String
    Dim is_matched As Boolean
    
    If (Trim(uname) = (Text1.Text)) Then
        MsgBox "username exists"
        Else
        MsgBox "username does not exist"
        
        is_matched = False
    
    Open "G:\College Work ------- Updated 2008 March\new-work\savedfile.txt" For Input As #1
        Do While Not EOF(1)
            Input #1, uname
            If Trim(uname) = Trim(Text1.Text) Then
                 is_matched = True
                 Close #1
                 Exit Do
            End If
        Loop
    Close #1
    Ofcourse i modified it, setup a new form for tests, not working how I wanted,

    When I type username in text1 (lets say one that exists in txt file), it still says username does not exist :S, hmm, I still remain testing this, until someone can put me right

  12. #12
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: [RESOLVED] how to stop it from writing the same details

    I would just load the usernames into an array, then when the user types their username do something like:


    Code:
    'Load users
    Dim intFileNum as Integer, intIndex as Integer
    Dim strUsers() as string
    
    intfileNum = FreeFile
    open [file] for input as intfilenum
    do while eof(intfilenum) = false
    
    redim preserve strUsers(intIndex) Input #intfilenum, strUsers(intindex) intIndex = intIndex + 1
    Loop 'Check Username for intIndex = lbound(strUsers()) to ubound(strUsers())
    if strUsers(intIndex) = txtUser.text then
    msgbox "User found!" 'Your Code
    Else
    msgbox "User Not Found!"
    end if
    Next intIndex

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