I'm struggling with a code I found on Stackoverflow. It keeps on telling me:
run-time error "424: object required".
I want to double-click on a record within a list box to open a form to the specific record clicked. The value from the record that I need the form to navigate to is found in Column 1 of the list box.
List Box Name: frmDashboardJBCreate
Field name in the table: JobcardNumber (numeric field)
Form Name: frmJobcardCreate
Table Name: tabJobcard_Issue
The code I tried is the following; but it keeps on giving me the error mentioned above:
DoCmd.OpenForm "frmJobcardIssue", , , _
"[JobcardNumber] = '" & Me.frmDashboardJBCreate.Column(1).Value & "'"
Could you please assist me?
If JobcardNumber is numeric, don't use single quotes around the parameter.
ListBox.Column() is zero-based, so if you want the first column, it's
DoCmd.OpenForm "frmJobcardIssue", , , _
"[JobcardNumber] = " & Me.frmDashboardJBCreate.Column(0).Value
Note: frm usually stands for "Form", so it is rather confusing if you name a listbox frmDashboardJBCreate. Are you sure you have that name right?
Related
I have two forms, one which is a data entry form, the other is a summary form which lists all the records in the database. On the summary form there is a listbox which lists all the records. I want the user to be able to select a record from the listbox and using a command button, open the second form to the specific record. I've gotten this work with using a specific field, in one case "Name", using the code below:
DoCmd.OpenForm "frmEditAddPerson", acNormal, ,"[PersonName]='" & Me.listPeople.Value & "'"
but when I realized two people could have the same name, I decided it made sense to use the PersonID which is the primary key and its datatype is "AutoNumber". I can't seem to get this to work:
DoCmd.OpenForm "frmEditAddPerson", acNormal, "[PersonName] = " & SelectPersonID
Note, I am getting SelectedPersonID by pulling it from ListBox from a hidden column. I confirmed that I am infact getting correct the number value for the AutoNumber field by displaying it in a MessageBox while trying to debug.
For the WHERE argument of this method/command, I know you are supposed to contain String values in quotes, Integers without, and dates with "#" like in a SQL statement. I've tried delcaring SelectedPersonID as a string and as an integer and I still cannot get the above to work. I've even tried the below just to be sure:
DoCmd.OpenForm "frmEditAddPerson", acNormal, "[PersonName] = " & CInt(SelectPersonID)
Each time I get "Type mismatch". Is the AutoNumber field special in the sense that it cannot be used for something like this or does it need to be handled in a special way?
If PersonID is the autonumber primary key, reference it in the fourth OpenForm argument (WhereCondition). Your OpenForm examples still include PersonName instead of PersonID.
Also, in your last two examples, PersonID was referenced in the third OpenForm argument (FilterName).
DoCmd.OpenForm "frmEditAddPerson", acNormal, , "[PersonID] = " & SelectPersonID
It can be easier to keep track of which option is which by including the option names.
DoCmd.OpenForm FormName:="frmEditAddPerson", View:=acNormal, _
WhereCondition:="[PersonID] = " & SelectPersonID
I'm making a search form for keyword searches. I have comboboxes with values ["And","Or","Not"], comboboxes with a list of field names, e.g. ["Title","Description",...] and textboxes associated with each combobox. I'm trying to create the sql query based on the values of the comboboxes and text box. To do that, I need to refer to the field name based on the value of the combobox. I'm trying solutions I've found searching, but I'm still getting the error: "Compile error: Sub or Function not defined". The debugger highlights "VHS_Metadata_Aug52014(field)" in yellow.
.....
bool_type = Me!Controls!dropBoolType2.value
field = Me!Controls!dropSearch2.value
value = Me!Controls!txtKeywords2.value
If (value) > 0 Then
SQL_query_string = SQL_query_string & " " & bool_type & " " & VHS_Metadata_Aug52014(field) = " & value"
End If
.....
I'd use dot notation instead. Using bangs like that doesn't let you use variables very easily. Try something like
Forms("formname").controls(foo).value
Where foo is the value from the combobox.
I'm not sure what VHS_Metadata_Aug52014 is though, if it's the name of the form you were trying to reference, you were partly there.
I have almost finished a project and I would like some user options in the form.
I would like to allow the user to select a product code and when selected, it will find the relative record in the table and then display all the information from that in a report.
Any ideas on the best approach to this would help me out massively.
Use the combo box selection with DoCmd.OpenReport as the WhereCondition option.
So if the report's record source table or query includes a numeric field named product_code and your combo box is named cboProductCode ...
DoCmd.OpenReport "YourReport", _
WhereCondition:="[product_code] = " & Me.cboProductCode
If the field is text rather than numeric, add quotes around the value in the WhereCondition.
WhereCondition:="[product_code] = '" & Me.cboProductCode & "'"
I have an access form, i want to know how to add item in combo box if there is not in there.
my combo box is in value mode.
Unfortunately you cannot change the rowsource permanently without changing the form to design mode and adding the new value. It is possible to do this with code, but it is not a good idea when people are working. The easiest thing is to create a small table and add the values to that. New values will then be saved when you close the form.
Allen Browne has a description of how to do this : http://allenbrowne.com/ser-27.html
This is one of the ideas he shows:
Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new product category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO Categories ( CategoryName ) " & _
"SELECT """ & NewData & """ AS CategoryName;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
You'll need to set the property limit to list to true.
Then add some code in the On Not In List event that potentially adds the value to the combo box. Here is a tutorial. Or you can view the other answer.
Please note that it is usually better to utilize a table that stores the values for your combo box. With a value list, unless you disable the shortcut menus a user can right click on the combo box select Edit List Items... and modify the list even if it is set to limit to list... which effectively defeats any limitation you are trying to place on the field.
My Goal:
A form field (in MS Access) that is has some drop down choices. If the wanted value isn't in the lookup table, the user should be able to add it by typing it in.
Let's suppose the lookup table has rows: A, B, C, D. The user wants "E" which doesn't exist yet. Ideally they'd "override" and type in "E", which would then be added to the lookup table for future entries.
My google-fu has failed on this. Is there a term for this that I should be using? What are some good approaches? (I've been playing with combo-box and its wizard so far).
Thank you for any suggestions!
Aha, solved my own here:
http://allenbrowne.com/ser-27-01.html
Access 2007
To use the new properties in Access
2007:
Open your form in design view.
Right-click the combo, and choose Properties.
On the Data tab of the Properties box, set Allow Value List
Edits to Yes, and List Items Edit
Form to the name of the form to use
for adding items to the list.
When you are using this form, you can
now right-click the combo, and choose
Edit List Items.
There is also advice for older versions of Access.
You can try the following code:
Private Sub Combo33_NotInList(NewData As String, Response As Integer)
Dim strSql As String
If MsgBox(NewData & " not in list, add?", _
vbYesNo + vbQuestion) = vbYes Then
strSql = "insert into tblStudents (name) values(" & NewData & ")"
CurrentDb.Execute strSql
Response = acDataErrAdded
End If
End Sub
Note I used a table name of Students, and field name of Sname. So, just
change the table name, and the field to whatever you used.