Setting Auto Increment Start Value as a Variable in Access - variables

I've looked around online but can't find any clear answers for this. Is it possible to set the starting value of an Auto Increment field to a variable (example to follow) in MS Access VBA?
Private Sub Command10_Click()
Dim dbs As Database
Set dbs = CurrentDb
Dim tblTempMinID As String
tblTempMinID = "DMax(EntryID, tblCalendar) + 1"
dbs.Execute "ALTER TABLE tblTemp ALTER COLUMN EntryID AUTOINCREMENT(tblTempMinID)"
End Sub
I'm currently getting an error in field definition with the above code. Still doing research, but I figured it wouldn't hurt to put this out here.

Assuming you want to increment by 1:
dbs.Execute "ALTER TABLE tblTemp ALTER COLUMN EntryID AUTOINCREMENT(" & tblTempMinID & ",1)"

Related

MSACCESS - How to change the control display property of a table field using ALTER COLUMN

I have a function that has to change a table field into a BIT type and i also want to set its control display property to listbox, so i tried like this but don't know how to continue..
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "ALTER TABLE tblStore " _
& "ALTER COLUMN Storage BIT"

MS Access Database insert values on a column using vba and sql query

Is there any way to add a column into an MS Access database using VBA and SQL query and then insert values using formulas from other column.
As an example, I want to create a column employee and it should have calculated values on all the records. I want to use LEFT(col2, 3) in the employee field.
This is my code:
Sub createcolumn()
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase("H:\VBA\Array Practice\Database1array.accdb")
objAccess.CurrentProject.Connection.Execute ("ALTER TABLE Company ADD COLUMN employee CHAR ")
End Sub
Sub insertvalues()
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase("H:\VBA\Array Practice\Database1array.accdb")
objAccess.CurrentProject.Connection.Execute "INSERT INTO Employee (Gross) VALUES LEFT([full_name], 3)"
End Sub
I am able to create a new column but could't insert the value.
You can directly update the table using column FULL_NAME instead of inserting. I have tested the following code and it is working.
Sub test()
Dim dbs As Database
Set dbs = OpenDatabase("Database1.accdb")
dbs.Execute "UPDATE EMPLOYEE SET GROSS=LEFT(FULL_NAME, 3)"
dbs.Close
End Sub
You added a column to a table named Company, but you are attempting to insert values to a table named employee.
If the table named company as records then an update statement is what you need to add values into the new column employee, but if the table does not have any record, then an insert statement can be used to add values into the employee field.

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

How to increment ID by 1 in SQL Server (backend) when INSERT code is written in vba (frontend)?

I am new to this forum.
I have the following environment: an Access frontend client that is connected to a database on SQL Server (backend). I would like to use an Access form to enter data that is associated with a specific ID number (in database Table). Ideally the ID will automatically increment when an INSERT is made to the Table. The vba code (based on the SQL query) I wrote is the following:
Option Compare Database
Option Explicit
Private Sub List0_Click()
Dim HelloWORLD As String
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
CurrentDb.Execute "INSERT INTO Table1 (TESTING_1, TESTING_2) VALUES (" & 9 & "," & HelloWORLD & ")"
End Sub
The code is compiling but it is not appending the Table1 like it is supposed to do. Please Help.
You can use sequences.
You can creates sequence MySequence1 and use it:
CurrentDb.Execute "INSERT INTO Table1 (TESTING_1, TESTING_2) VALUES (NEXT VALUE FOR MyDemoSequence," & HelloWORLD & ")"
Can you make the ID field an IDENTITY field within SQL Server? This will automatically increment whenever a new row is inserted. Example from Microsoft documentation:
CREATE TABLE new_employees
(
id_num int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)
The first number is the seed, and the second is the increment, so IDENTITY(1,1) means start at '1', and increase by '1' each time.
You can retrieve the ID that was created using SELECT SCOPE_IDENTITY(); following the insert if necessary.

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