at the first, I should explain my scenario to declare what I will do:
I want to merge records in one Table and then save them in another table.
Why?
I had a very complex query which needs long time to run and give back the result. I decided to divide this query into two query and save the resulat of the first query into a table and then the second query use the new generated table
the source table is often updated and the generated table must be generated too
If I use this code on my VBA this error occured:
Error 3010 Table already exist
Set db = CurrentDb
ssql = "SELECT DISTINCT tb_KonzeptDaten.DFCC, " _
& "tb_KonzeptDaten.OBD_Code AS Konzept_Obd,tb_KonzeptDaten.DFC " _
& "INTO Test_Table FROM tb_KonzeptDaten"
db.Execute ssql, dbFailOnError
RecordsUpdated = db.RecordsAffected
have you any idea ? do we have another SQL statement to do this job?
You can delete the table in VBA if it already exists and then run the query.
tblName = "TableX"
tbl = DLookup("Name", "MSysobjects", "Type=1 and Name='" & tblName & "'")
If Not IsNull(tbl) Then
DoCmd.DeleteObject acTable, tbl
End If
Or
Set db = CurrentDb
ssql = "DELETE FROM Test_Table"
db.Execute ssql, dbFailOnError
ssql = "INSERT INTO Test_Table SELECT DISTINCT tb_KonzeptDaten.DFCC, " _
& "tb_KonzeptDaten.OBD_Code AS Konzept_Obd,tb_KonzeptDaten.DFC " _
& "FROM tb_KonzeptDaten"
db.Execute ssql, dbFailOnError
Related
In Access I have a form with a series of checkboxes that correspond to columns in a table and would like to export only the selected columns to Excel with some columns always being exported.
I think the logic should be something like:
CurrentProject.Connection.Execute "SELECT Date, Country, Retailer INTO temp FROM table1 WHERE 1=0"
If optionSKU.Value = True Then
CurrentProject.Connection.Execute "ALTER TABLE temp ADD COLUMN SKU FROM table1"
'repeat for all options
End If
CurrentProject.Connection.Execute "INSERT INTO temp("All relevant data from table1")"
DoCmd.OutputTo acOutputTable, temp, acFormatXLSX, thePath, True
I would just create the SQL, then execute it and export the temp table:
Sql = "Select [Date], Country, Retailer"
If Me!CheckBoxField1.Value Then
Sql = Sql & ", Field1"
End If
If Me!CheckBoxField2.Value Then
Sql = Sql & ", Field2"
End If
' etc.
If Me!CheckBoxFieldn.Value Then
Sql = Sql & ", Fieldn"
End If
Sql = Sql & " Into Temp From Table1"
CurrentProject.Connection.Execute Sql
I have a syntax issue in the first CREATE TABLE statement.
I'm receiving the following VBA error:
Run-time error '3290'
The goal is to move the distinct data to a new table dependent on values in specific columns. Afterwards the original table is cleared, and every distinct value will be inserted again. The temporary table will be deleted afterwards.
' ** Issue here ** '
db.Execute ("CREATE TABLE tTemp AS (SELECT DISTINCT History_Date, Sedol, Selskabsnavn, MarketCap, JQScore, JQ_Rank, Value_Rank, Quality_Rank, Momentum_Rank FROM JQHistory)")
db.Execute ("DELETE * FROM JQHistory")
db.Execute ("SELECT * FROM tTemp INTO JQHistory")
db.Execute ("DROP TABLE tTemp")
This code is being run from within MS Excel.
Final solution:
db.Execute ("SELECT DISTINCT History_Date, Sedol, Selskabsnavn, MarketCap, JQScore, JQ_Rank, Value_Rank, Quality_Rank, Momentum_Rank INTO tTemp FROM JQHISTORY ORDER BY History_Date")
db.Execute ("DELETE * FROM JQHistory")
db.Execute ("ALTER TABLE JQHistory ALTER COLUMN Id COUNTER (1, 1)")
db.Execute ("INSERT INTO JQHistory SELECT * FROM tTemp")
db.Execute ("DROP TABLE tTemp")
You already have the syntax to make this work in the third statement, although it's not quite correct.
The first line should be
db.Execute ("SELECT DISTINCT <list of fields> INTO tTemp FROM JQHistory")
The third statement should be:
db.Execute ("INSERT INTO JQHistory SELECT <list of fields> FROM tTemp")
I have the following code:
Private Sub Command134_Click()
Dim strInsert As String
Set db = CurrentDb()
strInsert = "INSERT INTO [SEMP Documentation] (Staff_Name) VALUES " & (Staff_Name.Value) & ");"
MsgBox (strInsert)
Debug.Print staffname
db.Execute staffname, dbFailOnError
End Sub
Staff_Name is a combo box on a Microsoft Access form that has the name of 10 or so individuals, each with their own index.
The message box has the Staff_Name.Value call producing the index rather than the actual text name. For reference, the values in the combo box are pulled from a table, where the first column is the staff's index and the second column is the staff's name.
The user picks which person they are.
Secondly, the db.Execute statement fails; the table name is SEMP documentation that I am trying to insert into - "The Microsoft Access database engine cannot find the input table "
any help appeciated
try this
"INSERT INTO [SEMP Documentation] (Staff_Name) VALUES ('" & (Staff_Name.Value) & "');"
2nd prob is
db.Execute staffname, dbFailOnError
should be db.Execute strInsert, dbFailOnError
i am trying to append some records from a certain table to a new table ,
when i try it i am unable to append to the values
to the new table i.e "Table1" in this case what might be the reason for the data not getting appended to the table?
From what I can tell from your code you are just appending records from one table in your database into another table in the same database. You don't need to use a Recordset object to accomplish this; an INSERT INTO query will accomplish what you want.
Dim strSQL as String
Dim db as DAO.Database
Set db = CurrentDb
strSQL = "INSERT INTO Table1 ( UPI, COMPANY ) " & _
"SELECT INPUT_TBL.ID, INPUT_TBL.COMPANY FROM INPUT_TBL"
db.Execute strSQL, dbFailOnError
Set db = Nothing
Note that I used CurrentDb for the db object, under the assumption that the current database is the same one at C:\Users\test.accdb. If that's not the case, then you will need to do one of two things:
If you can add both of these tables as linked tables to the database you are running this code in, you can just use the same code as above. Instructions on how to add tables from an external database to your current database are available here: http://office.microsoft.com/en-us/access-help/import-or-link-to-data-in-another-access-database-HA001227658.aspx#BM3 and many other places on the internet.
If you can't add linked tables to your database for some reason, you can add IN clauses to the previous SQL string, which would become:
Dim strSQL as String
Dim db as DAO.Database
Dim dbname as String
Set db = CurrentDb
dbname = "C:\Users\test.accdb"
strSQL = "INSERT INTO Table1 ( UPI, COMPANY ) IN """ & dbname & """ " & _
"SELECT INPUT_TBL.ID, INPUT_TBL.COMPANY FROM INPUT_TBL IN """ & dbname & """"
db.Execute strSQL, dbFailOnError
Set db = Nothing
This code would allow you select the data from Table1 in the external database and add it to INPUT_TBL in the same external database.
I am fairly new to using VBA in MS Access and I am having trouble embedding SQL statements into VBA code. I have a database with almost 200 tables, and I would like to change the data type of one column (named lake) in each table to a "text" data type. I wrote the following code, but keep getting a syntax error for the ALTER TABLE statement.
Public Sub changeDataType()
Dim db As DAO.Database
Dim table As DAO.TableDef
Set db = CurrentDb
For Each table In db.TableDefs
DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"
Next
Set db = Nothing
End Sub
Can anyone tell me why I'm getting the syntax error?
Thanks,
Paul
this statement will not be correct:
DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"
if, for instance "table.Name" = "myTable", the resulting statement will look like this:
DoCmd.RunSQL "ALTER TABLEmyTableALTER COLUMN [lake] TEXT(100);"
try adding a space to separate the name, like this:
DoCmd.RunSQL "ALTER TABLE [" & table.Name & "] ALTER COLUMN [lake] TEXT(100);"