Unbound textbox compared to value in table, Datatype mismatch? - ms-access

I am currently working on a project for a class where I am building a database in MS Access and using a bit of SQL and VB for the first time. I am attempting to include a login feature to this project.
My form uses this code on a button labelled "login" to take in input from the user in unbound textboxes, named txtEmployeeID and txtPassword, and compare them values in table Employee_T, named EmployeeID and EmployeePW. EmployeeID is an integer value while EmployeePW is a string.
Private Sub Command1_Click()
If IsNull(Me.txtEmployeeID) Then
MsgBox "Please enter EmployeeID", vbInformation, "EmployeeID Required"
Me.txtEmployeeID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("EmployeeID", "Employee_T", "EmployeeID = ' & Me.txtEmployeeID.Value & ' "))) Or _
(IsNull(DLookup("EmployeePW", "Employee_T", "EmployeePW = ' & Me.txtPassword.Value & ' "))) Then
MsgBox "Incorrect LoginID or Password"
Else
End If
End If
End Sub
However, after pressing the login button I am met with an error: Run-time error '3464': Datatype mismatch in criteria expression.
Could someone point me in the right direction? I feel as though my error is in the way I am referencing my variables.
I have never looked at any visual basic before this project and I do not have a book/teacher/classmate to refer to as visual basic is not something we are specifically learning.
Thank you very much in advance for any insight, and apologies for any formatting errors. This is my first post.

It looks like you're concatenating vaues in your DLookup incorrectly.
'" & password & "' for strings
" & employeeID & " for integers
When compiled, these are going to look like :
string - '" & variable & "' -> Compiled -> 'YourVariableValue'
integer - " & employeeID & " -> Compiled -> 15

You need to escape your quotes like this: (be careful for using it exactly like I post it here)
If (IsNull(DLookup("EmployeeID", "Employee_T","EmployeeID = """ & Me.txtEmployeeID.Value & """"))) Or _
(IsNull(DLookup("EmployeePW", "Employee_T", "EmployeePW = """ & Me.txtPassword.Value & """"))) Then

Related

Access: Filtering form on field - fails when fields contains an apostrophe

I have a filter on a continuous form that uses a Combo Box to select records to match; the code is:
Private Sub SelectHospitalCbo_AfterUpdate()
Me.Filter = "[ContactHospital] = " & "'" & Me.SelectHospitalCbo & "'"
Me.FilterOn = True
End Sub
This was working fine until I discovered that if the ContactHospital field includes an apostrophe (e.g. Children's Hospital) I get an error message:
Run-time error '3075':
Syntax error (missing operator) in query expression '[ContactHospital] = 'Children's Hospital".
I understand why the error is occurring, but I can't find a workaround. A recent question on this forum seemed to have a similar problem to mine, but there were no answers. Does this mean I can't get around it?
In case anyone wants to suggest removing all the apostrophes form the hospital names, I would consider that, but unfortunately this database interacts with a (much larger) database where the hospital names can't be changed and have to match, so that's not an option for me.
Any help from more experiences Access developers appreciated!
Options:
filter by numeric hospital ID instead of its name
"[ContactHospital] = '" & Replace(Me.SelectHospitalCbo, "'", "''") & "'"
"[ContactHospital] = """ & Me.SelectHospitalCbo & """"
"[ContactHospital] = " & Chr(34) & Me.SelectHospitalCbo & Chr(34)

Eliminate Duplicate form entry linked to query result

I am new to VBA and Access and I have a query on a table which gives me only the active patients from my table as a result. The attached form allows my users to enter data into the table perfectly.
One of the fields for data entry on my form is the room number, which is not a unique field as far as the data in my table. However, on the query, this must be unique because there can not be two patients assigned to one room.
Is there a way to prevent users from assigning two active patients with the same room number using my query result? I have looked everywhere for an answer and the closest thing I have found was using DlookUp but I am unsure how this might work.
Thanks in advance.
Thanks Wayne for your answer, I made the compound key but noticed the user would have to add lots of information into the form and hit enter only to see no patient added to the list. That didn't really get me what I was after but it sent me looking in the right direction...
I finally came up with a code to find the room numbers entered into the form by using Dcount on the query result and placed this code in the onExit event of the room number so I would be able to send the focus back to the field until the correct answer is submitted by the user.
I already had the field validation set to ensure the correct range of room numbers would be entered. This now has the potential of getting the user stuck in an endless loop. So, I will have to add another if statement to the error msg to send the user out of the loop if they just can't think of an empty room number to place into the field.
Anyway, here is the solution I came up with.
First off, I added another text box "Me.txtResult" to my form to watch the results of Dcount which now isn't really necessary. However, it's in the code, so I thought I should explain that part.
Private Sub txtRoomNo_Exit(Cancel As Integer)
On Error GoTo Err_txtRoomNo_Exit
Dim strRmX As Integer
Dim strRmNo As Integer
strRmNo = Me.txtRoomNo
strRmX = DCount("[2017].[ID]", "2017 Query", "[2017].[Room#]=" & strRmNo)
Me.txtResult = strRmX
If strRmX > 0 Then
MsgBox "Sorry, it appears the room number you entered" & vbCrLf & _
" is already occupied by another patient" & vbCrLf & vbCrLf & _
" Click OK to try another room number.", vbCritical,
"Surgical Weaning Tool User Data Entry Error!"
Me.txtRoomNo.SetFocus
End If
Exit_txtRoomNo_Exit:
Exit Sub
Err_txtRoomNo_Exit:
MsgBox " Error: For Room# Entry Duplicate Check" & vbCrLf & _
" for New Patient Form" & vbCrLf & vbCrLf & _
" " & Error$ & " " & vbCrLf & vbCrLf & _
" Please write down ALL the above message and" & vbCrLf & _
" Inform Jim about this error" & vbCrLf & _
" As Soon As Possible. Thank You!" & vbCrLf & vbCrLf & _
" Please double check the room number you" & vbCrLf & _
" just entered is not alread in use!", vbCritical,
"Surgical Weaning Tool - - ERROR!"
Resume Exit_txtRoomNo_Exit
End Sub

How to reference control names dynamically in MS Access

I have the following code in an MS Access Form object.
Private Sub UpdatePMText(sLang As String)
'Used to pass both Mandate and Language Info to called Sub that will execute the queries
Dim iMandate As Integer
'Check if there is text in the box.
If Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = Null Or Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
iMandate = Me.txtMandateID
Call modUpdateText.macro_PMText(iMandate, sLang)
End Sub
If I refer to the Controls directly and simply type out the names of the forms, for example txtInput_PM_EN_DRAFT then the code works as intended.
The error message I get is that Access can't find the "Field" I'm referring to when I am on the first IF statement line. I have tried changing the .Controls to .Fields but that didn't work either.
I do not want to repeat the same code for every language I need to run. How do I reference control names dynamically in MS Access? What am I missing?
I think you need to add some basic troubleshooting. The answer is probably simpler than you think. It's likely you're just trying to lookup a textbox with mispelled name or it's failing on the Null comparison (as suggested by #HansUp)
I would try modifying your basic sub and testing it with this subroutine. As long as your code is in your current form and you're not referencing a subform your method will work.
Private Sub UpdatePMText(sLang As String)
Const ERR_MISSING_CONTROL As Long = 2465
On Error GoTo Err_UpdatePMText
Dim sTextBox As String
sTextBox = "txtInput_PM_" & sLang & "_DRAFT"
'Check if there is text in the box.
If Nz(Me.Controls(sTextBox).Value, "") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
Exit Sub
Err_UpdatePMText:
If Err.Number = ERR_MISSING_CONTROL Then
MsgBox "Error: No Such Textbox: " & sTextBox
Else
MsgBox "Error Looking Up Textbox: """ & sTextBox & """" & vbCrLf & Err.Description
End If
End Sub

i cant pass the value from table to textbox through dlookup vba ms access

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] & "'")

Filter in Access form

I have a form and I want it to be filtered just after it loads.
After I click on the form it should be able to load by filtering specific data.
I want it to filter by Program Nam and Year.
I have tried the following code but I keep getting syntax errors:
Private Sub Form_Load()
Combo5.Value = Form_0_Cover.Combo0
Combo7.Value = Form_0_Cover.Combo2
'Me.Filter = "[Program_Name]=" & Me.Combo7 & " AND [Budget_Year]='" & Me.Combo5 & ""
End Sub
I am not sure what the problem seems to be. I keep getting syntax error.
Try:
Me.Filter = "[Program_Name]='" & Me.Combo7 & "' AND [Budget_Year]=" & Me.Combo5
I suspect that program name is text and budget year is numeric. It is possible that the program name combo has an id as the bound column, in which case things might get a little more difficult, probably:
Me.Filter = "[Program_ID]=" & Me.Combo7 & " AND [Budget_Year]=" & Me.Combo5