Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work - vba

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.

Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

Related

access a specific field within a query that is part of a subform

I am trying to get an understanding of what the syntax would be to access a specific field within a query that makes up a subform within another form.
Form > SubForm/Query > Field
The field I am trying to get access to a field called Berthed that operates as a Boolean field. Either the vessel is present or it is absent.
This is what I was trying and it does not work.
Me.Boat_Move_Prior_Week.SourceObject.berthed
In this case here is the breakdown
Me = current form
Boat_Move_Prior_Week = query
Berthed = field I am trying to access through the VBA
My final goal will be to create an If statement, that upon requery of the Form, will look at the Berthed field, and if the field is checked (vessel is present) do nothing, but if the field is unchecked (vessel is not present) then remove the vessel from the query.
To reach the field (on the subform) bound to the field berthed, this is the syntax (assuming the textbox on the subform also is named berthed):
IsBerthed = Me!NameOfSubformControl.Form!berthed.Value
Here an amazing guide to know how to deal with controls in main form and subforms:
Link
Aside from my syntax issues I should have been using False for the value. Thanks to the two posters above for getting me in the right direction.
If Me.Boat_Move_Prior_Week.Form!BERTHED.value = False Then
Me.Boat_Move_Prior_Week.SourceObject = ""
End If

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

How to reference tables/queries/reports in access using an alias name?

In my Access database, I have tables with the naming convention of "t_customers" or "t_orders". I have a combo box which lets me pick from these options. For a better look, I want to have the options in the combo box without the "t_", so one of the options is just "customers". Then in VBA, I can access the combo box's value of "customers" and then pass that as a parameter to my function which will export the contents of the associated table "t_customers" to an excel file.
Is this possible to do? I can think of using a select case statement and for each combo box value I manually assign the related table value, but there has to be a better way. Is there some sort of short cut that does this in Access using a relationship or similar feature without coding? Or is there a simpler way to code this?
EDIT:
I am also trying to get the value of the current item selected in my combo box named "cbTable". I have no macros attached to the combo box. I have a button that when pressed it runs a macro which calls my VBA function. In the function I have this code:
Dim cbValue As String
cbValue = cbTable.Value
MsgBox (cbValue)
I get this error:
Runtime Error '424'
Object Required
The error is on the
cbValue = cbTable.Value
line. What is wrong with this code and how do I get the current value of the combo box?
Thanks in advance.
You can set your RowSource for your combobox to
SELECT MSysObjects.Name, Replace(MSysObjects.Name,"t_","") AS Expr1
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0));
Then set the Column Count to 2, the Column Widths to 0;2 (the second number just needs to be any positive number) an the Bound Column to 1.
Now when you go to call your function the value of the combobox will be the real name of the table, t-customers, but what you saw on your form was customers.
As for accessing that combobox a few things need to be in place.
Let's say you have a form, Form1, and your combobox, cbTable, is on that form.
So when your form runs it looks like this
Now let's say you have an OnChange even for that combobox
Notice that when you type cbTable it appears in the intellisense (ctrl+space). This tells you that your object is accessible at this scope.
Now if you were in another module where that variable is out of scope you would get this error when you try to compile.
Because Module1 has no idea what cbTable is. However, you can reference anything so long as it's fully qualified. So if we run this test with your form open, and "orders" selected all 3 of these lines are equivalent
Forms("Form1").cbTable
Form_Form1!cbTable
Forms("Form1").Controls("cbTable")
Notice at the top of all my modules it says Option Explicit. This forces your code to basically be checked for syntax/scope validity before you run it. Tools > Options

How to allow a combobox to be left blank//skipped on a form

I am relatively new to Access and any SQL and VBA I have picked up is self-taught, so I would really appreciate answers that are not too heavily laden with technical terms...that said, I am having an issue with allowing comboboxes to be left blank on a form if the user chooses not to input data. Also, I am using Access 2016.
Initially the problem I ran into was that if a combobox was entirely skipped and left blank, or if the user selects the combobox and then tries to move on without making a selection from the list, they got the error "You tried to assign the Null value to a variable that is not a Variant data type," and were unable to move on to any other fields on the form or to save the record being entered.
I found this article that details a possible solution, but I can't seem to get it to work. I made an unbound combobox, set the Row Source to:
SELECT EmailID, PersonalEmail FROM EmailT UNION SELECT "<N/A>","<N/A>" FROM EmailT
ORDER BY PersonalEmail;
where PersonalEmail is a field of type short text and the EmailID is an autonumber. I also followed the article's steps for formatting the combobox (column width, etc.) and set it to Limit to List = Yes.
Here is my exact code:
Private Sub Combo62_AfterUpdate()
If Combo62 = "<N/A>" Then
EmailID = Null
Else
EmailID = Combo62
End If
End Sub
Private Sub Form_Current()
If IsNull(EmailID) Then
Combo62 = "<N/A>"
Else
Combo62 = EmailID
End If
End Sub
< N/A> now shows up on my list, but if it is selected I get the error: "The value you entered isn't valid for this field. For example, you may have entered text in a numeric field or a number that is larger than the FieldSize setting permits."
Access's debugger highlights the line:
EmailID = Null
but I am not sure what steps I should take now to try and fix this.
I am completely open to other methods of allowing the combobox to be left blank if someone knows of a better way to do this also. For all I know, I am missing something really obvious! Thanks in advance for any insight!
EDIT: Thanks for your help guys, I never did figure out what exactly the problem was, but I got some advice from another forum to rethink my database design so this ended up being a null issue--it's all totally different now!
If the recordsource for your form contains a query with one-to-many relationships this error may come up. Try to use only the base table as the recordsource for the form. Use subforms if you need to display related data. The rest of the code may then be unnecessary.

Microsoft Access 07 VBA: Using Text Box or Combo Box as a Control to hide Columns

I currently have a form. The form contains a subform that displays a Query. The Form also has 2 Text Boxes and buttons for both Boxes. The first box acts as a filter for the query to filter specific record. The second box is intended to be used to hide columns in the query. My issue is that my code will not recognize the text box, or any outside source from my form as a Field Name to be found. Here is my current code:
Private Sub Command137_Click()
Forms![Vermont]![Query1 subform].Form.[Query1 Field Name].ColumnHidden = True
End Sub
Currently if I replace "Query1 Field Name" with any field name that exists in the query the column will hide. However if replaced with anything else I recieve the following error:
"Runtime Error '2465'
Microsoft Access Can't Find the Field '|' reffered to in your expression"
I am pretty sure that I am not referencing the Form Control correctly. I have tried replacing [Query1 Field Name] with the following:
[Text142.Text]
[=Text142]
[Text142]
[Forms![Vermont]![Text142]]
I am very new to VBA but I definitely feel as if this is an easy fix; if possible.
Thank you in advance for any help!
Subform references are always a bit weird. This should work, though:
Forms![Vermont]![Query1 subform]![Text142].ColumnHidden = True