I have the following code in my Access VBA program but it is giving me an error when I run the code.
I created table with Table1 of name and I wanted to insert a new record into the table.
But the code had a error.
The Error is like this.
Object variable or With block Variable not set
Sub Insert()
Dim db As Database
Dim StrSQL As String
StrSQL = "INSERT INTO Table1 VALUES ('1', 'Fuji')"
db.Execute (StrSQL)
db.Close
End Sub
The simplest method is to take advantage of CurrentDb:
Sub Insert()
Dim StrSQL As String
StrSQL = "INSERT INTO Table1 VALUES ('1', 'Fuji')"
CurrentDb.Execute StrSQL
End Sub
And if the first field is numeric, no quotes:
StrSQL = "INSERT INTO Table1 VALUES (1, 'Fuji')"
Related
I just found a strange behavior i Microsoft Access. When i run the code below i would expect that rs.EOF would be true. It is not. It turns out that any number of spaces is equal to any other number of spaces. Why this strange behavior?
Sub tmp()
Dim rs As DAO.Recordset
CurrentDb.Execute "create table table1 (id counter, field1 text(10),primary key (id))"
CurrentDb.Execute "insert into table1 (field1) values ('')"
Set rs = CurrentDb.OpenRecordset("select * from table1 where field1 =' '", dbOpenSnapshot)
Debug.Print rs.EOF
rs.Close
End Sub
I have Access dba with a staging table where I store all table names.
Table name: SourceTable
tbl name field: SourceID
key field: ReferenceID
I would like to create SELECT query where "FROM table" is actually a dynamic table name based on SQL query. Something similar to this:
Select * from 'select SourceID from SourceTable where ReferenceID=1';
You might be able to do something with VBA to accomplish this.
Put a combobox that gets its items from your SourceTable on a form
Add a textfield on the form with your reference ID
Add a button on the form with the following On_Click code
Select the table name, enter your Reference ID, and click the button
Private Sub cmdQuery_Click()
Dim qdf As QueryDef 'Query definition containter'
Dim mySQL As String 'SQL Statement container'
Dim t As String 'table name containter'
Dim rid As String 'ReferenceID container'
'getting values for your table and ReferenceID'
t = cboTableNames.Value
rid = txtRID.Value
'building SQL string'
mySQL = "Select * from " & t & " WHERE ReferenceID=" & rid & ";"
'Delete the query if it already exists'
DoCmd.DeleteObject acQuery, "NewQuery"
'building query'
Set qdf = CurrentDb.CreateQueryDef("NewQuery", mySQL)
'running query'
DoCmd.OpenQuery qdf.Name
'closing query'
qdf.Close
'clearing memory'
Set qdf = Nothing
End Sub
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);"