Link to home
Start Free TrialLog in
Avatar of navynuke04
navynuke04

asked on

Selecting data from table and storing it in a global variable.

I'm trying to write some code that will ask the user for their username and password. Once it validates this, it should select information about that user and store it in global variables to be used later.

In a module, I've created the following global variables:
Option Compare Database

Global g_txtUserName As String
Global g_txtLastName As String
Global g_txtFirstName As String
Global g_intGroup As Integer

On the login form, there is a login button which runs the following code when clicked:
Private Sub Command4_Click()
    Dim rs As Recordset
    Set rs = CurrentProject.Connection.Execute("Select count(*) from tblLogin where Username='" & txtUser & "' and Password = '" & txtPassword & "'")
   
    If Not rs.EOF Then
        rs.MoveFirst
        If rs(0) > 0 Then
            '***store user's info in global variable for later use***
            g_txtUserName = txtUser
            g_txtLastName = CurrentProject.Connection.Execute("Select Lastname from tblLogin where Username = g_txtUserName ")
            g_txtFirstName = CurrentProject.Connection.Execute("Select Firstname from tblLogin where Username = g_txtUsername ")
            g_intGroup = CurrentProject.Connection.Execute("Select Group from tblLogin where Username = g_txtUsername ")
            '******
            Dim stLinkCriteria As String
            Select Case g_intGroup
            Case 1
                DoCmd.OpenForm "frmAdminPanel", , , stLinkCriteria
            Case 2
                DoCmd.OpenForm "frmManagementPanel", , , stLinkCriteria
            Case 3
                DoCmd.OpenForm "frmNormalUserPanel", , , stLinkCriteria
            Case Else
                MsgBox g_txtUserName, " has not been assigned to a group. Please report this error to the administrator."
            End Select
            DoCmd.Close acForm, Me.Name, acSaveNo
            Else
            MsgBox "Login Incorrect"
        End If
        rs.Close
    End If
End Sub

This code is based on code someone else wrote for me. It validates the username and password and sets the g_txtUserName global just fine. It crashes on the next line when it tries to set the g_txtLastName global. It gives me a type mismatch error.

I'm pretty new to programming in VB, so I'm sure I'm probably missing something pretty simple.
Avatar of flavo
flavo
Flag of Australia image

declare your vaialbes like

Public myString As String

not

global myString As String
Avatar of navynuke04
navynuke04

ASKER

It works with g_txtUserName declared as a global. Why wouldn't it work for the others?

I made the changes you described, and it still crashes at the same place with the same error.
ASKER CERTIFIED SOLUTION
Avatar of flavo
flavo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome! Changing it to use dLookups worked. I just had to add a comma between "tblLogin" and "Username =

Like I said, I'm pretty much a beginner at VB. Thanks for your help!
Glad you sused it out

>> I'm pretty much a beginner at VB

I think you're better than a beginner :-)

Dave