When setting user permissions to open up a particular form for a particular user I am getting a Runtime error from the DLookup statement - vba

I am trying to restrict the availability of a form to be opened by a user. the following code picks up on the correct user access type and the correct form name, however i am getting the runtime error 2471. I would appreciate some help here as i cannot see what is wrong?
The error statement in this case is: Run-Time error '2471': The expression you entered as a query parameter produced this error: 'Stock'. When i go to debug it brings me to the DLookup and i cannot see what is wrong.
Watch expression:Watch : : "Employeeaccesstype =" & cable & " " & "AND FormName=" & thisform : "Employeeaccesstype =0 AND FormName=Stock" : String : Form_Stock.Form_Load
Private Sub Form_Load()
Dim cable As String
cable = TempVars("AccessType")
Dim thisform As String
thisform = Me.Form.Name
If DLookup("Hasaccess", "tblemployeeaccess", "Employeeaccesstype =" & cable & " " & "AND FormName=" & thisform) = False Then
DoCmd.Close
MsgBox "You Do Not Have Access"
End If
End Sub
All help will be most appreciated as i am on a deadline to finish this

If your fields Employeeaccesstype and FormName are text, then the values in the Dlookup need to be wrapped in single quotes. Additionally, you should cater for the possibility that the record doesn't exist in the table (returning a Null value from the DLookup):
If Nz(DLookup("Hasaccess", "tblemployeeaccess", "Employeeaccesstype ='" & cable & "' AND FormName='" & thisform & "'"),False) = False Then
You should also consider not actually allowing them to open the form in the first place.
Regards,

I changed tack here and went for a different approach. I created a new table to hold user login details and a query to join details on tables and referenced that on the Dlookup rather than trying to reference the login form through a variable. Witth those changes made i changed the DLookup to
If DLookup("Hasaccess", "AccessQuery", "HasAccess =false and Formname = 'Part' ") =
False Then
DoCmd.Close
MsgBox "You Do Not Have Access"
End If
This is now working correctly. Many thanks o Applecore for the assistance with this problem

Related

DoCmd.OpenForm with a Like in Access is not working for me

I'm pretty new at Access and I am trying to open a form with a like filter using the code
Private Sub AddCOB_Click()
Dim FilterLike As String
If Nz(JobSwitch, 0) = 0 Then
MsgBox ("Please Select a Job")
JobSwitch.SetFocus
Else
FilterLike = "[ID] = LIKE '" & jobNUMBER & "*'"
DoCmd.OpenForm "AddCOF", , , FilterLike
End If
End Sub
I keep getting Runtime error 3075
jobNUMBER is a public variable declared in a module and defined in this form, that seems to be working fine. This message is showing what I believe is the right syntax. I also checked it with a MsgBox. I cannot figure out what I am doing wrong. Can someone please show me the error of my ways?
= and LIKE are different comparison modes. Access expects you to use one or the other.
In your case, use [ID] LIKE
FilterLike = "[ID] LIKE '" & jobNUMBER & "*'"

Setting Form Field to autopopulate with previous record entry for text, date, and combo box

I am still very new to most vba coding. I'm trying to do pretty much the exact same thing as with this post:
Microsoft Access Form Setting Default Value to Previous Entry for Both Text Boxes and Drop Down Lists
but nothing from it seems to be working for me. I want to populate each field in the form with the previous record's data via a button Autofill_Click()
Private Sub Autofill_Click()
Me!DateTimeID.DefaultValue = Me![DateTimeID].Value
Me.frmDate.DefaultValue = "#" & Me.frmDate & "#"
Me.Location.DefaultValue = "'" & Me.Location & "'"
End Sub
I'm receiving the error code "type mismatch" for the first line with DateTimeID, as well as an error code "There is an invalid use of the . or ! operator or invalid parentheses." for the second line with frmDate. The third line isn't even throwing an error code, but it isn't populating the desired field (or any field, for that matter) with what I want.
Any help would be much appreciated.
For text:
Me.[DateTimeID].DefaultValue = "'" & Me.[DateTimeID] & "'"
For date/time:
Me.frmDate.DefaultValue = "#" & Me.frmDate & "#"
For number:
Me.Quantity.DefaultValue = Me.Quantity
Code is usually put in control's AfterUpdate event so user doesn't have to do anything other than normal data entry/edit.

MS Access - Main menu form with 2 text boxes used to open and filter a different form - getting a type mismatch (Run error 13)

I have a main menu where users type in a name of a station (text box) and the name of a cohort (text box) and it would open a new form based on these values. For example, if "New York" was entered for a station and "1b" was entered for cohort, then the form would filter to only show data that have both. However, I am getting a data mismatch error.
The fields text boxed at my main menu are called "detailed_s_station" and "detailed_s_cohort". The names of the respective fields in the form I want to filter these values are called "station" and "cohort".
I can get this to work if there are only one set of criteria (e.g., if I just search for the cohort or just search for the station), however something is going on with my AND here. Any help is appreciated to help me get rid of this data mismatch error.
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "'" And "[cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
The problem that you are having is how you are joining the string together. Instead try:
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "' And [cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
If you ever have problems like this in future, a quick Debug.Print strLinkCriteria22 would show you the contents of the string that is causing problems.
Regards,

Run-Time error 3075 Access Syntax Error for combobox Search in Access

I am learning about how to create a searchbox with combo box. I was learning with a video in youtube :
Access: How to Create Search Form Using Combo box Part 1
However, when I do my code it doesn't work. :/ I get the Run-Time error 3075 Access Syntax Error.
Private Sub cboVendorSearch_AfterUpdate()
Dim MyVendor As String
MyVendor = "Select * from Vendors where ([vend_name] = " & Me.cboVendorSearch & ")"
Me.Invoices_subform.Form.RecordSource = MyVendor
Me.Invoices_subform.Form.Requery
End Sub
Assuming vend_name is a text field, need apostrophe delimiters.
MyVendor = "SELECT * FROM Vendors WHERE [vend_name] = '" & Me.cboVendorSearch & "';"
Instead of setting RecordSource, an alternative is to set Filter property.
Me.Invoices_subform.Form.Filter = "[vend_name] = '" & Me.cboVendorSearch & "'"
Me.Invoices_subform.Form.FilterOn = True
Would probably be better to use vendor ID for search criteria. Explore multi-column combobox where the ID field is a hidden column but combobox uses the hidden column for its Value yet displays vend_name to user.

update table through form

This is the next attempt I am making at solving this problem. In my timeline is the prior attempt.
I have three tables BOAT, BERTHAGE, LOCATION. These tables are joined in the following way in a query called Boat_Move
BOAT(boat_ID) joins to BERTHAGE(boat)
LOCATION(Loc_ID) joins to BERTHAGE(location)
This query is viewed on the form as a subform.
When the form opens up I will highlight the vessel which needs to have its location moved/updated
Once highlighted in the subform I click the edit button
Private Sub cmd_Edit_Click()
If Not (Me.Current_Week_Boat_Move.Form.Recordset.EOF And Me.Current_Week_Boat_Move.Form.Recordset.BOF) Then
With Me.Current_Week_Boat_Move.Form.Recordset
Me.txt_Boat = .Fields("BOAT_NAME")
Me.txt_Cur_Loc = .Fields("LOCATION")
Me.txt_Cur_Flo = .Fields("FLOAT")
Me.txt_Cur_Doc = .Fields("DOCK")
Me.txt_Cur_Ori = .Fields("ORIENTATION")
Me.cmd_Add.Caption = "Update"
Me.cmd_Edit.Enabled = False
End With
End If
End Sub
This puts the appropriate information into the text boxes.
I've also did up the following with the idea that this would update the location(field) in the BERTHAGE(table)
Private Sub cmd_Add_Click()
Dim strSQL As String
strSQL = "INSERT INTO BERTHAGE(BOAT, LOCATION)" & _
" VALUES(" & Me.txt_Boat & ",'" & Me.txt_Cur_Loc & "');"
CurrentDb.Execute "UPDATE BERTHAGE " & " SET LOCATION=" & Me.txt_Cur_Loc & _
" WHERE LOCATION=" & Me.txt_Cur_Loc
Current_Week_Boat_Move.Form.Requery
cmd_Clear_Click
End Sub
I'm not sure if in this instance I should be creating a new table to make the update and then overwriting the older table. The issue I see here is the older table is at around 24k records so that doesn't seem like the best way forward.
So at this point I'm facing two problems, and I am unsure if solving either one will solve the actual problem of updating the BERTHAGE(location) field.
View of the from
Any help is appreciated.