Access VBA filter by User - vba

I'm trying to setup a simple filter button that will filter by the network user ID which is populated in the [Owner] field when a record is created. The idea is to see only the records you created.
the code I'm using is as follows:
Private Sub FilterUser_Click()
Dim Uname As String
Uname = Environ("USERNAME")
Me.Tasks.Form.Filter = [Owner] = Uname '<---- Error debugs this line
Me.Tasks.Form.FilterOn = True
End Sub
I get the following error but I'm confused on what field it thinks is missing.
Any help would be greatly appreciated.

Filter requires a string value.
me.tasks.form.filter = "[owner]='" & uname & "'"

Related

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.

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

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

How to get a form field to auto fill when a product is entered?

I am trying to get my form to populate the days needed for testing automatically when I enter a new record but it keeps erring out. I am very new to using VBA and Access 2016.
I have looked at some other examples that people have posted that work and cannot get it to work.
I am continually getting debugger.
Option Compare Database
Private Sub Fill_SKU_AfterUpdate()
PopulateFields
End Sub
Private Sub PopulateFields()
frmSerialTracerLog.Days_Used_For_Off_Test = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = '" & frmSerialTracerLog.Fill_SKU & "'")
End Sub
You probably are referring to the current form, thus use Me:
Private Sub PopulateFields()
Me!Days_Used_For_Off_Test.Value = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = " & Me!Fill_SKU.Value & "")
End Sub

Access VBA decode

I am new to VBA code, and working on a project to revised a built MS Access database that has some VBA code built from another person. Can some one explain what the code below would do? My end goal to to create a Data Entry form that would keep previous value from some fields when the users enter the next New Record. Thank you so much.
'Private Sub DocumentTypeCombo1_AfterUpdate()
'Me.DocumentNameCombo1 = Null
'Me.DocumentNameCombo1.Requery
'Me.OrderBy = "Errors DESC"
'Me.DocumentNameCombo1 = Me.DocumentNameCombo1.ItemData(0)
'End Sub
'Private Sub DocumentNameCombo1_AfterUpdate()
'Me.SubcategoryCombo1 = Null
'Me.SubcategoryCombo1.Requery
'Me.OrderBy = "SubCategory DESC"
'Me.SubcategoryCombo1 = Me.SubcategoryCombo1.ItemData(0)
'Me.DocumentNameCombo1.Tag = Me.DocumentNameCombo1
'End Sub
It is commented out, so it does nothing. Even if not, it wouldn't as it misses this line:
Me.OrderBy = "Errors DESC"
Me.OrderByOn = True
If you include the line, it will rearrange the sorting of Me.
To ...
keep previous value from some fields when the users enter the next New Record
you would set the DefaultValue (always a string value) in the AfterUpdate event:
Me!SomeControl.DefaultValue = Chr(34) & Me!SomeControl.Value & Chr(34)

Runtime Error 3061 Help (ms access)

I've been wracking my brain trying to find what is wrong with this query and I just don't see it. I'm trying to open a record set and I keep getting runtime error 3061: "Too few parameters: Expected 1."
Here's my code...
Dim ansRs As Recordset
Dim qRs As Recordset
Dim ansQuery As String
Dim qQuery As String
Dim i As Integer
qQuery = "Select * From TrainingQuizQuestions Where TrainingQuizID = (Select TrainingQuiz.TrainingQuizID From TrainingQuiz Where QuizName = Forms!MainMenu!txtVidName);"
ansQuery = "Select * From TrainingQuizQuestAns"
Set qRs = CurrentDb().OpenRecordset(qQuery)
Set ansRs = CurrentDb().OpenRecordset(ansQuery)
I'm getting the error from the line "Set qRs = CurrentDb().OpenRecordset(qQuery)". I copied and pasted that query into access and ran it and it returned exactly what I want to get in my record set, yet when I run it in VBA I get the error. Am I missing something really simple? Any help would be greatly appreciated.
First ensured that your form is open, then put the form reference outside your quotes.
qQuery = "Select * From TrainingQuizQuestions Where TrainingQuizID = " _
& "(Select TrainingQuiz.TrainingQuizID From TrainingQuiz Where QuizName = '" _
& Forms!MainMenu!txtVidName) & "';"
The form value is not available to a recordset used in VBA.