Update combobox based on another combobox through query - vba

I have several combo boxes (CB) populating from my access database. They all pull from the same table and use the same hidden primary key from the table. When I select a value from one of the CB's I would like to have the others updated with the associated value based on the matching primary key.
Is this possible?
I've been trying to use variations of the following for a while now with no success:
Dim strSQL As String
strSQL = "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Me.cboGSRTask.Section = CurrentDb.OpenRecordset(strSQL)
Debug.Print "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Thank you.

If you open a recordset, you need to read a value from it, you can't use the recordset as value.
I guess what you are looking for is:
Dim strSQL As String
Dim RS As Recordset
strSQL = "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Set RS = CurrentDb.OpenRecordset(strSQL)
' This assumes that gsr_id is the bound column of cboGSRTask
Me.cboGSRTask = RS!gsr_id
RS.Close
Or instead all of the above, using DLookup() :
Me.cboGSRTask = DLookup("gsr_id", "task", "task_wid = " & Me.cboTask.Column(0))
Or it might be even easier to add gsr_id to the row source of cboTask (as a column with width = 0) and use that column to assign to cboGSRTask.

Related

Access VBA After Insert Autofill?

I'm looking for a little help/input on how to have a field auto-populate after an identification number is entered in a new record.
I have an entry screen ("RetailEntry") where someone will enter a "Pack Number" once they either move to another record I'd like the "Description" field to populate with the corresponding "Description" ("qryDescriptions") for that pack number.
Example:
If I enter pack number 6781234 it would give me the Description "Test"
I'm trying to figure out the best way of doing this and I thought maybe a record loop like below where it runs a query for the pack number and description then loops through the records to fill it in. It kinda works if I only enter one pack number but adding anymore pack numbers or copy and pasting multiple pack numbers it errors with a "No Current Record" which I'm guessing has to do with the order of processes. I am really hoping there is a way to do this where it will autofill vs me having to add a button to make this work.
Any thoughts, advice or help is greatly appreciated!!
Code:
Private Sub Form_AfterInsert()
Dim db As Dao.Database
Dim Desc As String
Dim PDesc As String
Dim rs As Dao.Recordset
Dim rq As Dao.Recordset
'Set Description
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset("RetailEntry")
Set rq = CurrentDb.OpenRecordset("qryDescriptions")
PDesc = rs.Fields("Pack_Number").Value
Desc = "SELECT DISTINCT PackNum, Description " _
& " FROM PIC704Current " _
& " WHERE Packnum = '" & PDesc & "'"
strSQL = Desc
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
db.QueryDefs.Delete "qryDescriptions"
Set qdfPassThrough = db.CreateQueryDef("qryDescriptions")
qdfPassThrough.Connect = "ODBC;DSN=SupplyChainMisc;Description=SupplyChainMisc;Trusted_Connection=Yes;DATABASE=SupplyChain_Misc;"
qdfPassThrough.ReturnsRecords = True
qdfPassThrough.ODBCTimeout = 180
qdfPassThrough.SQL = strSQL
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Loop version
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
rq.MoveFirst
Do Until rs.EOF = True
If rs.Fields("Description").Value <> rq.Fields("Description").Value Then
Else
Me!Description.Value = rq.Fields("Description").Value
End If
rs.MoveNext
rq.MoveNext
Loop
End If
End Sub

Need button on form to insert field names of a table into another table in Access

There are similar questions and answers on here, but I am such a VBA beginner that I don't have the ability to make changes to a code to make it work for me. Ideally this is what I need to create in an Access database.
On a form there is a text field that has a table name in it.
A pressed button inserts that table and its field names into a separate table (call it TableFields for ease, with columns "TableName" and "FieldName").
Is this possible? If this is answered elsewhere, can someone point me in the right direction? Thank you in advance!
It seems like this post is similar, but not specific/dumbed down enough for me:
How to List Field's Name in table in Access Using SQL
Use a combobox or listbox that lists tables.
Code in button Click or combobox AfterUpdate event like:
Dim rst As DAO.Recordset, db As DAO.Database, x As Integer
If Not IsNull(Me.cbxTable) Then
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM [" & Me.cbxTable & "] WHERE ID=0")
For x = 0 to rst.Fields.Count - 1
db.Execute "INSERT INTO TableFields(TableName, FieldName) " & _
"VALUES('" & Me.cbxTable & "','" & rst(x).Name & "')"
Next
End If
Alternative code.
Dim rst1 As DAO.Recordset, rst2 As DAO.Recordset, x As Integer
If Not IsNull(Me.cbxTable) Then
Set rst1 = CurrentDb.OpenRecordset("SELECT * FROM [" & Me.cbxTable & "] WHERE ID=0")
Set rst2 = CurrentDb.OpenRecordset("SELECT * FROM TableFields WHERE ID=0")
For x = 0 to rst1.Fields.Count - 1
rst2.AddNew
rst2!TableName = Me.cbxTable
rst2!FieldName = rst1(x).Name
rst2.Update
Next
End If

Adding/Updating a sql table in VBA

I am trying to make it so that Query 1 inserts into 1 of the 3 fields based on whether the task value (grabbed from another table) is selected (true or false check box). This part works.
Query 2 is supposed to update the other 2 fields that are empty to the values in a combo box. This part is not working. Syntactically it seems correct and it isn't crashing. But I just don't think it is reading it in the first place.
All this is supposed to be accomplished via 1 button click.
Query 1:
Sub DBInsert_SkillMatrix()
sSQL = "Insert into tblSkillMatrix (Task) Select Task from tblTask WHERE Selected = true"
Set db = CurrentDb
db.Execute (sSQL)
db.Close
End Sub
Query 2:
Sub DBUpdate_SkillMatrix()
Dim db As DAO.Database
sSQL = "UPDATE tblSkillMatrix SET Skill = '" & Me.cboAST_Skill & "', Requirement = '" & Me.cboAST_Requirement & "' WHERE Skill = ''"
Set db = CurrentDb
Debug.Print (sSQL)
db.Execute (sSQL)
End Sub
The error is in the wrong where clause. It should be with ISNULL. Here is a guidance how to use it - https://msdn.microsoft.com/en-us/library/ms184325.aspx

My query for counting subrecords in Acces does not work?

I'm having a problem with my query. I'm having a database with a Table "Label" that contains some subrecords("ReleaseID").
What I want to do: I want to count the subrecords that are linked to the LabelID in my Label table. I have the following code:
Dim db As Object
Dim rst As Recordset
Set db = CurrentDb
Dim qryCount As String
Dim Value as integer
qryCount = "select count(ReleaseID) as aantal from(select LabelID, ReleaseID from Label where LabelID = " & Me!LabelID")
Set rst = db.OpenRecordset(qryCount, dbOpenDynaset)
Value = rst!aantal
The query is working when I try it in the Query design in Acces. But when I use it in VBA then it doesn't.
Appreciate any help.
It's hard to see your actual code when you don't format it.
This is what you last posted
qryCount = "select count(ReleaseID) as aantal from Label where LabelID = " & Me!LabelID
Set rst = db.OpenRecordset("Label", dbOpenDynaset)
Value = rst1!aantal
If that's what you actually have then you
Probably can't use Value - I think that's a reserved word
You've set the recordset name to rst but you're trying to use rst1 for value reference
I'm not sure if its just a copy mistake, but the closing brace is missing, isn't it?
qryCount = "select count(ReleaseID) as aantal from(select LabelID, ReleaseID from Label where LabelID = " & Me!LabelID & ")"
If your are just looking for a count you can do it with DCount
varCount = DCount("ReleaseID", "Label", "LabelID = " & Me!LabelID)
No recordsets required. If you're not iterating through the recordset Dcount is much simpler and less taxing on the system.
It's called a domain function, well worth looking up if you have a spare moment (Dlookup, DSum, Dmax, Dcount).
Why not use DCount:
Dim Value As Long
Value = DCount("*", "ReleaseTable", "[LabelID] = " & Me!LabelID.Value & "")

How to set controlsource of a textbox from SQL

I have a subform bound to a SQL statement. Inside the subform, I have a few text boxes bound to the fields of this SQL. However, I have another text box that needs to be bound to a field from a different SQL statement with criteria from the first one. My code looks like below:
Dim subform As Object
Dim formFilter As String
formFilter = "SELECT * FROM my_table_1"
Set subform = Me!my_subform.Form
subform.RecordSource = formFilter
subform.field1.ControlSource = "tb1f1"
subform.field2.ControlSource = "tb1f2"
...
subform.f3.ControlSource = "= SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4"
I cannot use a DLOOKUP function here directly, because I need to sort the table result.
Thanks in advance for your help.
I think I would simply create a little function to go get the result you want. It would probably be best to simply rework DLookup in your own function and add sort but I won't do that here.
First the form code. Notice I am not setting the recordsource, just the value which may not be what you want.
subform.f3 = fGet_tb2f3([tb1f1], [tb1f2])
Then the function code (put in your own error handling)
Public Function fGet_tb2f3(tblf1 as String,tblf2 as String) as String
Dim rst as dao.recordset
Dim db as database
set db = currentdb
set rst = db.openrecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & tb1f1 & "' AND tb2f2 = '" & tb1f2 "' ORDER BY tb2f4",dbopensnapshot
if not rst is nothing then
if not rst.eof then fGet_tb2f3 = rst!tb2f3
rst.close
set rst = nothing
end if
db.close
set db = nothing
end Function
You can't bind controls on the same form to 2 different recordsets. The only thing you could do is pull data from 2 different recordsets, but that's probably not the best way to do anything.
In order to do that, you'd have to create a second recordset and grab that value in it. Something like:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4")
Me.MyControlName = rec(0)