Selecting data from table and storing it in a global variable.
Posted on 2004-11-24
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
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
DoCmd.OpenForm "frmAdminPanel", , , stLinkCriteria
DoCmd.OpenForm "frmManagementPanel", , , stLinkCriteria
DoCmd.OpenForm "frmNormalUserPanel", , , stLinkCriteria
MsgBox g_txtUserName, " has not been assigned to a group. Please report this error to the administrator."
DoCmd.Close acForm, Me.Name, acSaveNo
MsgBox "Login Incorrect"
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.