Get values from text/combo boxes on form to different table - vba

Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Private Sub Command208_Click()
strSQL = "INSERT INTO PKR([Claim Number],[WANDFNF],[WANDOUP],[WANDEA]) VALUES ([Forms]![Form Level].[Form]![Claim Number],[Forms]![Form Level].[Form]![Wand_cmb1],[Forms]![Form Level].[Form]![Wand_cmb2],[Forms]![Form Level].[Form]![Wand_txt2])"
strSQL1 = "INSERT INTO PKR([TranFNF],[TranOUP],[TranEA]) VALUES ([Forms]![Form Level].[Form]![Tran_cmb1],[Forms]![Form Level].[Form]![Tran_cmb2],[Forms]![Form Level].[Form]![Tran_txt2])"
strSQL2 = "INSERT INTO PKR([DRFNF],[DROUP],[DREA]) VALUES ([Forms]![Form Level].[Form]![DR_cmb1],[Forms]![Form Level].[Form]![DR_cmb2],[Forms]![Form Level].[Form]![DR_txt2])"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL1
DoCmd.SetWarnings True
DoCmd.SetWarnings False8
DoCmd.RunSQL strSQL2
DoCmd.SetWarnings True
End Sub
Here i have build a button command_208 to get data from text/combo boxes on a form to different table PKR.
I am able to get data for 1st query but not getting any data for the strSQL1 & strSQL2.

Related

Loop Through SubForm Records In MS Access and Insert Into Table

I have an Access form with a subform and I'm trying to create a command that will run an Insert statement within VBA to insert data from the subform into another table. The problem I'm running into is that it duplicates the first record instead of inserting separate records. My code is below.
Private Sub Command52_Click()
Dim strSQL As String
Dim rs As DAO.Recordset
strSQL = "INSERT INTO DA_CC_QAQC_DRAWINGS_DETAIL (DRAW_ID, REV_DATE, REV_DESC, REV_TYPE)" & _
"VALUES ('" & Forms!frmASI!sfrmASI!cboDrawNumRef.Column(2) & "', Forms!frmASI.ASIDIFCDATE, Forms!frmASI!sfrmASI!ITEM_DESC, 'ASI');"
Set rs = Forms!frmASI!sfrmASI.Form.RecordsetClone
With rs
Do Until .EOF
DoCmd.RunSQL strSQL
.MoveNext
Loop
End With
End Sub
Since the SQL code refers to controls on form and does not cycle the form records, the same data is saved. Refer to and concatenate recordset fields instead of the embedded form controls. And the SQL would have to be within the recordset loop. As is, the recordset serves no purpose.
I presume code is behind the main form.
Private Sub Command52_Click()
Dim strSQL As String
Dim rs As DAO.Recordset
Set rs = Me.sfrmASI.Form.RecordsetClone
With rs
Do Until .EOF
strSQL = "INSERT INTO DA_CC_QAQC_DRAWINGS_DETAIL (DRAW_ID, REV_DATE, REV_DESC, REV_TYPE)" & _
"VALUES ('" & Me.sfrmASI.Form.cboDrawNumRef.Column(2) & "', #" & _
!ASIDIFCDATE & "#, '" & !ITEM_DESC & "', 'ASI');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
.MoveNext
Loop
End With
End Sub

Access VBA run query with values passed from a list box

I have made this form in Access and I am hoping to do the following task.
The list box here contains two columns, and can be multi-selected. I want to use the values second column (the right column) and pass them into a query that I set up for the "test2" button below.
And here is my VBA code for the on-click event for the button.
Private Sub test2_Click()
Dim db As dao.Database
Dim qdef As dao.QueryDef
Dim strSQL As String
Set db = CurrentDb
'Build the IN string by looping through the listbox
For i = 0 To Select_Counties2.ListCount - 1
If Select_Counties2.Selected(i) Then
strIN = strIN & "'" & Select_Counties2.Column(1, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE County_GEOID in " & "(" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strWhere
Set qdef = db.CreateQueryDef("User query results", strSQL)
qdef.Close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "User query results", acViewNormal
End Sub
I was getting this error:
Can someone tell me what I did wrong in the code? Thank you!
In this example from microsoft they call application.refreshwindow without explanation.
https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/database-createquerydef-method-dao
What I think is going on is that your code fails because access cannot find the query that was just added to it's collection of queries. Also your generated sql is no longer valid.
So: replace my sql with your own valid sql
Private Sub test2_Click()
Dim db As DAO.Database
Dim qdef As DAO.QueryDef
Dim strSQL As String
strSQL = "PARAMETERS GEOID Number; " 'without valid sql this code doesn't run so
'replace my sql with your own.
strSQL = strSQL & "SELECT GEOID FROM Counties"
Set db = CurrentDb
For i = 0 To Select_Counties2.ListCount - 1
If Select_Counties2.Selected(i) Then
strIN = strIN & Select_Counties2.Column(1, i) & ","
End If
Next i
strWhere = " WHERE County_GEOID in " & "(" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strWhere
Debug.Print strSQL
'now the important bit:
db.CreateQueryDef ("User query results") 'create the query
Application.RefreshDatabaseWindow 'refresh database window so access knows it has a new query.
'query will now be visible in database window. make sure to delete the query between runs
'Access will throw an error otherwise
Set qdef = db.QueryDefs("User query results")
qdef.SQL = strSQL
qdef.Close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "User query results", acViewNormal
End Sub

Using QDEF in VBA

I am trying to update a local table in an Access database using a value passed from a click event. I want to add checkmarks to a column in the table for all the same values as the record that was clicked. What am I doing wrong with the QDEF?
Public Sub addcheckmarks(functionLocation As String)
Dim strsql As String
Dim qdef As QueryDef
strsql = "PARAMETERS [CheckLocation] String; UPDATE tbl_TMP_inventory set addlocation = -1 where Location = [CheckLocation]"
Set qdef = CurrentDb.createQueryDefs("", strsql)
qdef!CheckLocation = functionLocation
qdef.Execute dbFailOnError
End Sub
You don't have to build a temporary query for a simple update:
Public Sub addcheckmarks(functionLocation As String)
Dim strsql As String
strsql = "UPDATE tbl_TMP_inventory SET addlocation = True WHERE Location = '" & functionLocation & "'"
CurrentDb.Execute strsql
End Sub

How to display a selection made on a list box in another Form in Access

I have create a combobox with a depending multiselection List box. I also have a button, wich I wanna asign an event on click.
I want this event to be able to show the selections the user did (combobox and multi selections on List box) in another Form to make a little table with more fields and more info.
This is the code i have so far, and trows me an "3265" error.
Any ideas?
Private Sub Command129_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("tblConfig Query")
For Each varItem In Me!List126.ItemsSelected
strCriteria = strCriteria & ",'" & Me!List126.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM tblConfig " & _
"WHERE tblConfig.TypeConfig IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "tblConfig Query"
Set db = Nothing
Set qdf = Nothing
End Sub
Thanks

Appending the data into the access table based on number of record in the table

I am new Access VBA,
My scenario is I want to pick the table name from another table
(In same access db (table name as parameter)).
Based on the Table name picked from parameter I need to pick le column from the same table(Parmater).Then i want to check distinct le from Parameter.
Based on distinct le (4 in my case) from parameter table i want to append the record to another table (ULAE).
The append statement would be as insert into with where condition as LE from parameter table.
Public Sub LinkTables()
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim stringsql As String
Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")
While Not rs1.EOF = True
MsgBox rs1![Table_name]
tblname = rs1!Table_name
stringsql = ("SELECT COUNT(LE) from [Table Valued Parameter]")
MsgBox tblname
DoCmd.SetWarnings False
DoCmd.RunSQL ("SELECT COUNT(LE) FROM [Table Valued Parameter]")
'Next i
DoCmd.SetWarnings True
NEW_TBLNAME = "t_00_unearned_unincepted_alloc_basis_table"
'MsgBox NEW_TBLNAME
'CurrentDb.TableDefs.Append New_tblname
'MsgBox rs1![le]
'Set TD = Nothing
'Dim stringsql As String
DoCmd.SetWarnings False
'stringsql = ("SELECT COUNT(LE) from " & tblname)
'DoCmd.RunSQL (stringsql,False)lename&
cnt = CurrentDb.TableDefs.Count - 1
MsgBox cnt
For i = 0 To cnt
lename = rs1![le]
MsgBox lename
DoCmd.RunSQL ("INSERT INTO " & NEW_TBLNAME & " SELECT * FROM " & tblname & " where le = '" & lename& "')
Next i
DoCmd.SetWarnings True
rs1.MoveNext
Wend
The code is giving error 2342 . CMDSQL requires sql statement.
Can someone suggest how can implement it
Thanks for the help
This was accomplished creating a Function.
Function LinkTables()
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim stringsql As String
Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")
While Not rs1.EOF = True
'MsgBox rs1![Table_name]
'MsgBox rs1![le]
tblname = rs1!Table_name
legal_entity = rs1.Fields(1)
NEW_TBLNAME = rs1![destination_table]
DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO " & NEW_TBLNAME & " SELECT * FROM " & tblname & " where le = '" & legal_entity & "'")
DoCmd.SetWarnings True
rs1.MoveNext
Wend
MsgBox "Tables Refreshed"
End Function