Mass-Update Form w/VBA & SQL - ID not pulling correctly, records not being written to temp table

amfrancis

Edited since original posting: I realized that I had declared the volId variable as an Integer, but it's being read as a string. Once I changed the declaration to "Dim volId As String" the SQL code appears to be getting generated properly. Now I just need help in figuring out why the records are not being inserted into the temporary table.

I am trying to create a form that will allow the user to create multiple work records without having to re-enter the date, hours, and category information. (Ex: 10 people worked the same shift at the holiday party.) The way I plan to do this is to create a temp table, write a complete record to the table for each volunteer selected, then do an INSERT query to select everything from the temp table and insert the records into the real Work_Records table. (This second portion is not done yet. I'm debugging as I go, and have gotten stuck with the first part.)

The problem is that my records do not appear to actually be getting inserted into the temp table. I'm guessing that something is wrong with my SQL code.

Thanks!

My code:

Private Sub qryAppendMassWorkRecords_Click()

On Error Resume Next
DoCmd.RunSQL "DROP TABLE Tmp"

'Declare Vars
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rstRecords As Recordset
Dim dynamicSQL As String
Dim strSQL As String
Dim strTable As String
Dim hrsWorked As Integer
Dim DateWorked As Date
Dim pgmWorked As Integer
Dim volId As String

'Set initial values
Set db = CurrentDb
Set qdf = db.QueryDefs("qryTempAppendTable")

'Create new temp table to hold values from the form
strSQL = "CREATE TABLE Tmp (VolunteerID VARCHAR(20), DateWorked DATETIME, HoursWorked INT, WorkCategory INT);"
db.Execute strSQL

'Grab the values for the static vars and assign them
For Each ctl In Me.Controls
    If ctl.Properties("Name") = "DateWorked" Then
        DateWorked = ctl.Value
    End If

    If ctl.Properties("Name") = "HoursWorked" Then
        hrsWorked = ctl.Value
    End If

    If ctl.Properties("Name") = "WorkCategory" Then
        pgmWorked = ctl.Value
    End If
Next ctl

'If combo box length > 0, create an INSERT statement to add the record to the temp table
For Each ctl In Me.Controls
    If ctl.ControlType = acComboBox Then
        'Category is the only combo box other than the ones for volunteer names; make sure it's not getting picked up
        If ctl.Properties("Name") <> "WorkCategory" Then
            'Verify that the field contains a name
            If Len(ctl.Value) > 0 Then
                volId = ctl.Value
                'Reset the dynamicSQL to the initial code & add form values
                 dynamicSQL = "INSERT INTO Tmp VALUES (" _
                    & "'" & volId & "', #" & DateWorked & "#, " & hrsWorked & ", " & pgmWorked & ");"

                 db.Execute dynamicSQL
            End If
        End If
    End If
    qdf.SQL = dynamicSQL
Next ctl

DoCmd.OpenTable "Tmp", acViewPreview

End Sub
jacouh

Try this:

dynamicSQL = "INSERT INTO Tmp VALUES ('" _
  & volId & "', #" & DateWorked & "#, " & hrsWorked & ", " & pgmWorked & ");"

It will get a good Access SQL string:

INSERT INTO Tmp VALUES ('15', #12/02/2013#, 5, 3);

In Access Database, Datetime must be quoted with sharp #, like: #mm/dd/yyyy hh:mm:ss#, or #yyyy-mm-dd hh:mm:ss#...

#12/02/2013#

More your VolunteerID is a string, so single quote like this:

'15'

Please notify, Access can only execute one single "INSERT" instruction, not multiple INSERT's as in MySQL. If you have many records, you must do a loop using VBA.

And run it to insert into Tmp, that you have not done:

db.Execute dynamicSQL

For example, this worked for me:

Sub qryAppendMassWorkRecords_Click()

  On Error Resume Next
  Dim strSQL As String
  Dim db

  'Set initial values
  Set db = CurrentDb

  strSQL = "CREATE TABLE Tmp (VolunteerID VARCHAR(20), DateWorked DATETIME, HoursWorked INT, WorkCategory INT);"
  db.Execute strSQL

  strSQL = "INSERT INTO Tmp VALUES ('242013', #12/2/2013#, 4, 39);"
  db.Execute strSQL

  DoCmd.OpenTable "Tmp", acViewPreview

End Sub

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert/Update mass records SQL Server

From Dev

All records being deleted when deleting records from a temp table

From Dev

#temp table not being dropped

From Dev

MySQL: mass update unique records

From Dev

SQL - add records to temp table until order is fulfilled

From Dev

SQL - add records to temp table until order is fulfilled

From Dev

SQL Loop not adding all input records to temp table

From Dev

Output not correctly being written to CSV

From Dev

SQL table pulling info

From Dev

Update via a temp table

From Dev

SQL Inserting into table from temp table, and having ouput id inserted into temp table

From Dev

Update data in a SQL Server temp table where the column names are unknown

From Dev

update SQL WHERE BOOKNO = BOOK number in temp table

From Dev

Temp Table in SQL Server

From Dev

SQL insert #TEMP table

From Dev

Is DocumentDB slower than SQL at pulling lots of records?

From Dev

GET form not being passed correctly

From Dev

In MS SQL Server 2005, what happens when a temp table is being accessed by different executions of the same SP?

From Dev

Create a temp table with data range using startdate and enddate from different records in SQL Server

From Dev

Updating Records in Temp table by using cursors

From Dev

Mass update of data in table - best method?

From Dev

mysql update table if record not in temp table

From Dev

Only the last record in sql table is being updated from a form

From Dev

Pulling ID from Table as soon as its assigned

From Dev

How to update records in table?

From Dev

Update table for records not existing

From Dev

NuGet Update-Database is not pulling new records in Visual Studio

From Dev

Combining 2 SQL Queries From A Single Table and Being Able to Update it

From Dev

SQL CTE vs Temp Table

Related Related

  1. 1

    Insert/Update mass records SQL Server

  2. 2

    All records being deleted when deleting records from a temp table

  3. 3

    #temp table not being dropped

  4. 4

    MySQL: mass update unique records

  5. 5

    SQL - add records to temp table until order is fulfilled

  6. 6

    SQL - add records to temp table until order is fulfilled

  7. 7

    SQL Loop not adding all input records to temp table

  8. 8

    Output not correctly being written to CSV

  9. 9

    SQL table pulling info

  10. 10

    Update via a temp table

  11. 11

    SQL Inserting into table from temp table, and having ouput id inserted into temp table

  12. 12

    Update data in a SQL Server temp table where the column names are unknown

  13. 13

    update SQL WHERE BOOKNO = BOOK number in temp table

  14. 14

    Temp Table in SQL Server

  15. 15

    SQL insert #TEMP table

  16. 16

    Is DocumentDB slower than SQL at pulling lots of records?

  17. 17

    GET form not being passed correctly

  18. 18

    In MS SQL Server 2005, what happens when a temp table is being accessed by different executions of the same SP?

  19. 19

    Create a temp table with data range using startdate and enddate from different records in SQL Server

  20. 20

    Updating Records in Temp table by using cursors

  21. 21

    Mass update of data in table - best method?

  22. 22

    mysql update table if record not in temp table

  23. 23

    Only the last record in sql table is being updated from a form

  24. 24

    Pulling ID from Table as soon as its assigned

  25. 25

    How to update records in table?

  26. 26

    Update table for records not existing

  27. 27

    NuGet Update-Database is not pulling new records in Visual Studio

  28. 28

    Combining 2 SQL Queries From A Single Table and Being Able to Update it

  29. 29

    SQL CTE vs Temp Table

HotTag

Archive