1

I'm trying to make a login/register system.

This is the registration UserForm.

Private Sub regReg_Click()
Dim TextFile As Integer
Dim FilePath As String

If regAParole.Text = "aparole" Then
    FilePath = ThisWorkbook.Path & "\accounts.txt"
    TextFile = FreeFile

    Open FilePath For Append As #1

    Print #TextFile, regID; regAmats; regParole

    Close TextFile

    MsgBox ("Registracija veiksmiga.")
    Unload Registracija
Else
    MsgBox ("Nepareiza administratora parole.")
End If
End Sub

The "aparole" thing is basically just a keyword to enter in a field so only administrators can create new accounts.

accounts.txt content looks like:

1DirectorPassword (ID+jobposition+password)

This is the authentication:

Private Sub logAuth_Click()

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim find As String
Dim result As Integer

find = logID & logAmats & logParole
FilePath = ThisWorkbook.Path & "\accounts.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
result = InStr(FileContent, find)


If result >= 1 Then

    MsgBox ("Autorizacija veiksmiga!") ' Success
    Unload Autorizacija
End If

Basically when logging in I search within the accounts.txt for the string combo (ID+jobposition+password) which I use when registering. So in general the approach works, but:

If I enter everything perfectly matched = works great

If I enter the password half of it, like in a format of = 1DirectorPass it still works, so basically how can I tell to only search for the whole string and not parts of it?

I think the issue lies within InStr...

4
  • Is "1DirectorPassword " a literal string, or is it actually something variable? And are the brackets that follow literal? It is not clear which is the format. Commented May 7, 2016 at 7:10
  • I have a UserForm with 3 TextBoxes. ID: textbox, JobPosition: textbow(with dropdown choices) and Password: textbox - whatever gets entered is saved into accounts.txt in a format of IDJobPositionPassword (1Directorpassword) Each entry is in a single line. Commented May 7, 2016 at 7:15
  • What you put between brackets is an example, right? The brackets are not actually there? Commented May 7, 2016 at 7:18
  • Yeah, no brackets. Just an example. Commented May 7, 2016 at 7:21

1 Answer 1

1

You could test for the newline markers in your file content, like this:

result = InStr(vbCrLf & FileContent, vbCrLf & find & vbCrLf)

This will only match complete lines. An extra newline is added before the file content so also the first line can be matched. At the end of the file content you would already have a vbCrLf character, because Print is supposed to add that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.