I am trying to validate a product code when entered in a textbox. I can't seem to figure out a way to throw up a message box saying nothing was entered.
Me.txt_Product = UCase(Me.txt_Product)
If Not IsNull(upProd) Then
If DCount("Validation_Test", "Validation_Testing_Table", "[Validation_Test] = '" & UCase(Me.txt_Product) & "'") >= 1 Then
MsgBox "User Name Found!"
ElseIf Me.txt_Product.Value = "" Then
MsgBox "You Did Not Enter a Product Code!"
Else
MsgBox "User Name Not Found!"
End If
End If
Any suggestions for help with the DCount or other ways to do this?
I suspect textbox is Null and testing for an empty string fails. Handle possibility of either Null or empty string:
ElseIf Me.txt_Product.Value & "" = "" Then
Concatenating Null with "" (empty string) returns empty string.
You can create a better logical flow:
Me!txt_Product.Value = UCase(Me!txt_Product.Value)
If Not IsNull(upProd) Then
If Nz(Me!txt_Product.Value) = "" Then
MsgBox "You Did Not Enter a Product Code!"
Else
If IsNull(DLookup("Validation_Test", "Validation_Testing_Table", "[Validation_Test] = '" & Me!txt_Product.Value & "'")) Then
MsgBox "User Name Not Found!"
Else
MsgBox "User Name Found!"
End If
End If
End If
And be careful with all those exclamation marks. Users will understand the message without being yelled at.
Related
I am trying to use DLookup to check a text field against a database value except it returns an error when I try to do so. MemberID is the username and is being found using txtUsername field and the password is obviously password which are both retrieved from the Member table.
Here is the code:
Private Sub btnLogin_Click()
If Me.txtPassword.Value = DLookup("[Password]", "Member", "MemberID =
Me.txtUsername.Value") Then
MsgBox "Access Granted", vbInformation, "CD Shop"
MsgBox "Welcome", vbInformation, "CD Shop"
DoCmd.Close
DoCmd.OpenForm "frmGymActivity"
Else
MsgBox "Please re-enter your Username and Password."
End If
End Sub
You need to use proper quoting and string concatenation, to make sure you're passing a string with the value you want.
If Me.txtPassword.Value = DLookup("[Password]", "Member", "MemberID = " & Me.txtUsername.Value) Then
I am assuming MemberID is a number.
I have a problem with Microsoft access for code I have written to lookup the USER TYPE field in the ADMINSTRATOR TABLE.It is not able to run due to syntax error 3075 missing expression operator in query expression 'USER TYPE'
It is used for my login menu to differentiate between two different user types and bring them to different forms.
The code works before I implemented the User Security code
Dim UserLevel As Integer
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
Here is my code:
Private Sub Command1_Click()
Dim UserLevel As Integer
If IsNull(Me.txtLoginID) Then
MsgBox "Please enter Login ID", vbInformation, "Login ID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("[User ID]", "ADMINSTRATOR TABLE", "[User ID]='" & Me.txtLoginID.Value & "' And Password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect Password or Login ID", vbInformation, "Login unsuccessful"
Else
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
DoCmd.Close
If UserLevel = 1 Then
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "MAIN MENU"
Else
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "ADMINISTRATOR FORM"
End If
End If
End If
End Sub
Field names and table names containing spaces or special characters (poor design choice) need to surrounded with square brackets.
I am trying to search a field called "Model" in a table called "ItemSpecs" but I cannot for the life of me figure it out and I have yet to be able to find any examples on google. In addition to searching the field I am trying to add different search methods (e.g. "Contains","Doesn't contain","Begins with", and "Ends with").
What I have so far
So far I the following but all it does is test which drop down item is selected (which works!).
What I still need
From what I have read I will need to query the table and put it into a vbRecordSet and then search that to get what I want? Which i think the skeleton code below will enable me to do that once I can figure out how to get the query results into the record set and then figure out how to query it.
Notes:
Thanks for the help! Imma keep working this damn thing but if someone can figure it out before me it would be great.
Private Sub Button_Search_Click()
If Len(Me.Search_Text.Value & vbNullString) = 0 Then
MsgBox "No Value"
Else
If Me.Search_Preferance.Value = "Starts with" Then
MsgBox "Starts with"
ElseIf Me.Search_Preferance.Value = "Ends with" Then
MsgBox "Ends with"
ElseIf Me.Search_Preferance.Value = "Contains" Then
MsgBox "Contains"
ElseIf Me.Search_Preferance.Value = "Doesn't contain" Then
MsgBox "Doesn't contain"
Else
MsgBox "Invalid Seach Preference Selected."
End If
End If
I'll assume that you are doing this on a form where the RecordSource is set to "ItemSpecs" (or a select query based off of that table where one of the included columns is "Model"):
Private Sub Button_Search_Click()
If Len(Me.Search_Text.Value & vbNullString) = 0 Then
Me.FilterOn = False
Me.Filter = vbNullString
Else
Dim Txt As String
Txt = Me.Search_Text.Value
Select Case Me.Search_Preferance.Value
Case "Starts with": Me.Filter = "Model Like """ & Txt & "*"""
Case "Ends with": Me.Filter = "Model Like ""*" & Txt & """"
Case "Contains": Me.Filter = "Model Like ""*" & Txt & "*"""
Case "Doesn't contain": Me.Filter = "Model Not Like ""*" & Txt & "*"""
Case Else
MsgBox "Invalid Seach Preference Selected."
End Select
Me.FilterOn = True
End If
End Sub
This is my first attempt to create a login form. I've read up a few forums about it and tried them myself. However, I've encountered the error when trying out the form.
"Run time error '2001': You canceled the previous operation."
Here's my code! The error highlighted is the DLOOKUP statement. When I move my cursor to LanID, it appears to be 0. (I guess it got something to do with it?)
Option Compare Database
Option Explicit
Private intLoginAttempts As Integer
Private Sub cmdLogin_Click()
'Check mandatory fields
If IsNull(Me.txtLanID) Or Me.txtLanID = "" Then
MsgBox "Lan ID is required.", vbOKOnly, "Required Data"
Me.txtLanID.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "Password is required.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Compare input password and database password
If Me.txtLanID <> "" Then
If Me.txtPassword = DLookup("Password", "tblUser", "[LanID]=" & Me.txtLanID) Then
LanID = Me.txtLanID
'Close Login Page
DoCmd.Close acForm, "frmUserLogin", acSaveNo
'Check whether user is an admin
If Me.txtAdmin = DLookup("Administrator", "tblUser", "[LanID]=" & Me.txtLanID) Then
If Me.txtAdmin = -1 Then
DoCmd.OpenForm "frmMenu"
Else
DoCmd.OpenForm "frmBack"
End If
End If
Else
MsgBox "Invalid Password. Try again.", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If
End If
'If user enters incorrect password 3 times
intLoginAttempts = intLoginAttempts + 1
If intLoginAttempts = 3 Then
MsgBox "You do not have access to the database. Please contact system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
If tblUser.LanID is text data type, enclose Me.txtLanID in quotes to avoid an error with your DLookup() expression. (You don't need .Value there because it's the default property.)
DLookup("Password", "tblUser", "[LanID]='" & Me.txtLanID & "'")
If that was not the explanation, test your DLookup() expression in the Immediate window (go there with Ctrl+g) to see whether it throws an error or what value it does return.
Note, when you test DLookup() in the Immediate window, use the current value of Me.txtLanID. For example, if Me.txtLanID contains the text "foo", test it like this ...
? DLookup("Password", "tblUser", "[LanID]='foo'")
I dont know why I can't pass the value of AccountType (from TblAccount) to Me.txtWAccounType (textbox from WithdrawView).
Pleaes help me, here's my code
If IsNull(DLookup("[AccountId]", "TblAccount", "AccountId = '" & txtWAccountId & "'")) Then
MsgBox "Account Number Doesn't Exist"
Else
Me.txtWAccounType.Value = DLookup("[AccountType]", "TblAccount", "AccountId = ' " & Forms![WithdrawView]![txtWAccountId] & "' ")
MsgBox "Account Number Do Exist"
End If
my goal is to retrieve that AfterUpdate of AccountId. messagebox, but an "Account Number Does Exist" pops up so it means there is a value, but why doesn't it appear in Me.txtWAccounType?
The problem is because there is no record that matches the criteria.. As you know there is a difference between '123' and ' 123' i.e. there is a 'space' character before the number based on your code.. Just replace the DLookUp as Follows..
DLookup("[AccountType]", "TblAccount", "AccountId = '" & Forms![WithdrawView]![txtWAccountId] & "'")