'CREATE TABLE' generates Run-time error '3290' - 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")

Related

How to add NUMBER column with default value?

I'm trying to add a column of the type NUMBER to my table using VBA. That is working for me. But i also want to set the default value for the column to 0, which is not working properly. I believe i need some help.
I do not want to alter a column, i want to add a column and at the same time set a default value.
The following code is working, it adds a column with nothing in it.
strSQL = "ALTER TABLE testTable ADD COLUMN testColumn NUMBER;"
dbs.Execute strSQL
But this is not working and gives an error.
strSQL = "ALTER TABLE testTable ADD COLUMN testColumn NUMBER DEFAULT 0;"
dbs.Execute strSQL
The error message i get is "Run-time error'3293': Syntax error in ALTER TABLE statement."
Default only works when it's used in DDL executed from an ADO connection. The syntax for this, which I have tested would be this:
CurrentProject.Connection.Execute "ALTER TABLE testTable ADD COLUMN testColumn NUMBER"
CurrentProject.Connection.Execute "ALTER TABLE testTable ALTER COLUMN testColumn SET DEFAULT 0"
Also, this only sets the default for the table for all new records, and does not modify existing records to 0. If that is also your intention, simply run an update query, like this one.
strSQL = "UPDATE testTable SET testTable.testColumn = 0;"
CurrentDb.Execute strSQL

Insert Into table from access form

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

Change data type of column in multiple tables in Access with VBA

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);"

Updating records in a Table

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

Insert multiple records in a single time using VBA Access

I am trying to avoid using multiple INSERT SQL queries but if I must I will. I have read over there that you can simply use INSERT INTO table(field) SELECT 'value' UNION ALL SELECT ... however I am having trouble with the syntax of it under MS Access 2007. I have tried a few variants but none works: I always get either "incorrect syntax" or "missing operator" error. I have tried with and without the ALL qualifier, with and without the SELECT keyword, etc. Here's how I am creating the query using VBA:
sql_query = "INSERT INTO " & tmp_tbl_name & " (transit)"
For Each xlCell In xlRange
sql_query = sql_query & " SELECT '" & xlCell.Value & "' "
sql_query = sql_query & "UNION"
Next xlCell
sql_query = Mid(sql_query, 1, Len(sql_query) - 6)
DoCmd.RunSQL (sql_query)
Here's a shortened sample of the query I am generating:
INSERT INTO tmp_tbl_2012_08_17_15_44_03 (transit)
SELECT 'L02_NA010001'
UNION
SELECT 'L13_PB010001'
UNION
SELECT 'L31_US020001'
UNION
SELECT 'L33_EX020010'
...
The table has only one VARCHAR(255) field and no relations to any other table (it's a temporary table to process a few things and keep only some elements of it).
Many thanks !
I think you can use good ol' DAO to do this rather quickly:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tmp_tbl_2012_08_17_15_44_03", dbOpenDynaset)
For Each xlCell In xlRange
With rs
.AddNew
.Fields("transit") = xlCell.Value
.Update
End With
Next xlCell
rs.Close
db.Close
It will probably be easier to run the statements one at a time in MS Access, the UNION will require FROM Table for each UNION statement, which means the FROM table will have to contain just one row, or a little fancy footwork.
INSERT INTO tmp_tbl_2012_08_17_15_44_03 (transit)
SELECT Transit FROM (
SELECT DISTINCT 'L02_NA010001' As Transit FROM tablename
UNION
SELECT DISTINCT 'L13_PB010001' As Transit FROM tablename
UNION ... )