Getting #Name? error in combobox - MS Access - vba

I'm trying to set the default value of a few items on my form so I don't have to keep updating them as I scan items to a table. I have 3 comboboxes in my form and the other two are working fine (keeping the same value after record entry) but the cbx for "Equipment" is consistently giving me this error. I've tried updating field names by going from "Type" to "Equipment Type" and now to "Equipment" and that has not fixed it. I'll try to give good context below, but please let me know if there is more information you need. Thanks! --Forgot to mention that the table for equipment types has one field: "EQType" and it is set as the primary key.
VBA to set default values in comboboxes
Private Sub Serial_Number_AfterUpdate()
[Equipment].DefaultValue = [Equipment]
[ToTech].DefaultValue = [ToTech]
[FromTech].DefaultValue = [FromTech]
End Sub
Field names of the table I'm updating:
Serial Number (Textbox)
Control source: Serial Number
Equipment (Combobox)
Control source: Equipment
Row Source: SELECT EQType FROM EquipmentTypes;
From Tech (Combobox)
Row Source: SELECT Roster.TechID FROM Roster;
To Tech (Combobox)
Row Source: SELECT Roster.TechID FROM Roster;
Date Added (Textbox) --Default Value
set to "=Date()"
Information about form:
Split form, named "Transfers", Record Source = Transfers table

DefaultValue is text, so you probably miss to quote the value:
Private Sub Serial_Number_AfterUpdate()
Me![Equipment].DefaultValue = "'" & Me![Equipment].Value & "'"
Me![ToTech].DefaultValue = Me![ToTech].Value
Me![FromTech].DefaultValue = Me![FromTech].Value
End Sub

Related

Why can't I get the current record ID from a multi-instance form in VBA

I have a database I'm working on with a number of forms, some of them have to be updated when something in a different form is changed. I've set it up so that they're multi-instance forms, which works something like this:
Set frm = New Form_Name
frm.RecordSource = "select * from Table_Name where id = " & ID
colForms.Add Item:=frm, Key:=frm.hwnd & ""
mintForm = mintForm + 1
frm.SetFocus
frm.Visible = True
colForms is a collection.
To refresh a form, I've added a function that refreshes the form that matches the name and ID passed:
Function RefreshForm(RForm As String, ID As Integer)
If Developer = False Then On Error GoTo Fehler
Dim F As Form
On Error Resume Next
For i = 1 To mintForm
Set F = colForms(i)
If F.CurrentRecord = ID And F.Name = RForm Then F.Refresh
Next i
'Error handling logic
Done:
Exit Function
Fehler:
RuntimeError "MultiInstance: RefreshForm", Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext, Err.LastDllError
Err.Raise 1000, , "Folgefehler"
End Function
Problem is, F.CurrentRecord is always 1 for some reason, even though the ID of the record in the form is definitely not 1. Any idea what I'm doing wrong, or how I could properly get the record ID (primary key) from the form? The form is definitely bound (at least the ones where I'm trying to get the ID).
CurrentRecord is not pulling record's ID from unique identifier field. CurrentRecord is a sequential number assigned by the form and has no relationship to the record unique identifier saved in field.
If focus is on fifth record then it is record 5 and if that record has a unique ID field value of 5 then they are equivalent. Doing this comparison seldom makes sense. A form always sequentially numbers records starting with 1 regardless of filtering/sorting yet the form could be filtered to display a subset of records and/or records could be sorted so ID's are not sequential nor start with 1.
If you want to know ID of record that has focus then reference field name holding that data.
If F!fieldname = ID And F.Name = RForm Then F.Refresh

How can a combobox store different type field values in MS Access database or have a dynamic column type

My goal is that the user chooses the type of field search and according to that another combo box show that available field:
CustomerID, CustomerFirstName, CustomerLastName, Phone are the columns of the Customer table.
If I choose customer ID in first the adjacent box shows:
When I choose first name as an option:
But after pressing Enter on "Dog", I get an error
The value you entered isn't valid for this field
The code for the combo box 2 is:
Private Sub SelctionBY_AfterUpdate()
Dim iVal
iVal = Me.SelctionBY.Value
Dim S As String
S = "SELECT " & iVal & ", CustomerID, CustomerFirstName, CustomerLastName, Phone From Customer"
Me.Combo33.RowSource = S
End Sub
So how do i fix this?
A combobox doesn't store anything, it's just for selection of something!
Data is stored in table fields and a combobox can be bound to a field to store data.
Your issue is that the bound field type changes, but it shouldn't. You can avoid that by binding the data field to the second column of the combobox, asCustomerIDis customer tables PK(Primary Key) that should be stored as FK(Foreign Key)
Me.Combo33.BoundColumn = 2

If Dlookup doesn't meet criteria in several tables then display error. Microsoft Access

Ok I could use some advice please as I am not frequently used to VBA.
I have a form and within that form, I have a subform that is built by a make table. I am in no need to change this.
I however would like to add a sanity check before pressing the upload button that updates SQL server. Where I am getting stuck is on a IIf(Dlookup) function; I am trying to look into a local table and if three fields in the local table match the fields and criteria I have on my subform then check to see if 5 columns within my subform add up to one other field in my subform.
For an example; In the Local table there are the three fields as: "UNIT", "TYPE", "Center"
"Unit" Criteria is as follows: 1212, 1213, 1214 and so on
"TYPE" Criteria is as follows: Shop, Outside, Innerbank
"Center" Criteria is as follows: Electrical shop, machine shop, outside shop.
*** What I need to do is match those fields from the table mentioned above which is called "Ledgend" to the subform on my form and if that Unit, Type, and Center are all present within a row on my subform then calculate(SUM) 5 fields on my subform and if it matches the field called Total on my subform then leave it as is but if it does not then highlight the rows that are different and throw an error msg
If DLookup("Unit", [dbo_TableName]) = "Unit", FORM![SUBFORM_NAME]!Unit & "TYPE", [dbo_TableName]) = "TYPE", FORM![SUBFORM_NAME]!TYPE, & _
"Center", [dbo_TableName]) = "Center", FORM![SUBFORM_NAME]!Center Then
subform Sum(Dis + ABS + Center + SS + WRK)
Else
MsgBox ("Error")
End If
Sum() is not used to add fields, it is used to aggregate records. Syntax for DLookup is completely wrong. Reference to subform makes no sense. Sounds like you need a nested If Then. Following assumes code is behind subform.
If Not IsNull(DLookup("ID", "Ledgend", "Unit & Type & Center'" = Me!Unit & Me!TYPE & Me!Center & "'")) Then
If Me!Total <> Dis + ABS + Center + SS + WRK Then
MsgBox "Error"
'code here to set BackColor property of each control
Exit Sub
End If
Else
Exit Sub
End If
BTW, Ledgend is a misspelling of Legend.
Could use Conditional Formatting rule to manage setting Backcolor property.

How to update text box based on combo box value

Beginner on Using MS access 2016 and VBA. I am trying to show information on how many items have been reserved or is in the reserve table based on what the user selects as the tool.
The tool combo box(cmbo_Tool) selects a tool from Table A. I need to count the amount of times this tool appears in Table B and then display it in textBox A.
I have made a query involving both tables, but i'm unsure on how to apply it to the label.
Instead I have used the AfterUpdate event on cmbo_tool and using the DCount option.
Another way I thought about is taking the Tool Id (in this case, say 5) from Table A, and searching for it in a column table B, and counting.
'using Dcount'
Private Sub cmbo_Tool_AfterUpdate()
Me.Text1404 = DCount("cmbo_Tool", "tbl_Booking", "Tool")
End Sub
'Using Table id'
Private Sub cmbo_Tool_AfterUpdate()
Dim T_var as integer
Dim FinalOut as integer
T_var = Me.cmbo_Tool.Column(0) 'This gives 5'
'I need to make T_Var link to Table b and count'
Me.Text1404 = FinalOut
End Sub
Using Dcount method, it gives a number im unsure of. I'm not even sure if im using dcount correctly.
Using the table id method, Im unsure how to take the value 5 and count it in table B, then display in the textbox.
I guess it could be something like this:
Private Sub cmbo_Tool_AfterUpdate()
Me!Text1404.Value = DCount("*", "tbl_Booking", "Tool = " & cmbo_Tool & "")
' If Tool is text, then use quotes:
' Me!Text1404.Value = DCount("*", "tbl_Booking", "Tool = '" & cmbo_Tool & "'")
End Sub
And do rename your controls to something meaningful.

Vba code to show selected data from combobox

I have an Access 2010 form which has a combobox listing three columns:
ID
first name
last name
The combo box is bound to a table containing this data. The ID column in the combo box is hidden, it only shows the first and last name.
When the user selects a row only the the first name is shown. in the property section, I chose:
Column Count: 3
Column widths:0;3,3
Bound Column: 1
I made another text field and in the combobox I wrote the following vbcode:
text=combo.value
that shows in the text field the chosen ID.
I want to show in another field (text\combo?) the last name.
How can I do that?
well I did it with recordset.
if someone know a simpler solution, I will be glad to lrn.
this is my code:
Dim dbs As ADODB.Connection
Dim id As String
Set dbs = CurrentProject.AccessConnection
Set rsRep = New ADODB.Recordset
Set rsRep.ActiveConnection = dbs
rsRep.Open "tblRep"
id = Me.cbPrvFirstName
rsRep.MoveFirst
rsRep.Find "RepId=" & id
Me.txtPrvLastName = rsRep.Fields(2)
rsRep.Close
You can pull in the value from another column within the combo box using the below code. By default combo.value will always give the value from the first column even if hidden.
try
text=combo.column(x)
in your case to retrieve last name it would be
text=combo.column(2)
where x is a NUMBER of the column you want to retrieve 0 being the first column
You could also try an alternative - This works like a VLookup in excel if you are familiar with that.
(Not as simple as the first option ;) )
text=Dlookup("[last name]","tblRep","RepId=" & combo.value)