Antero Duarte
Antero Duarte

Reputation: 345

How to include a variable when creating a new table through vba in MS-ACCESS

In my latest system design i came into a problem with the following idea:

I wanted to create a new table from the click of a button in a form, but i want the table to get it's name from the nrof variable that must get its value from a text field in the form, what i am trying right now, based on actual microsoft documentation is the following:

Private Sub Submit_Click()

Dim num As Integer
Dim nrof As Integer
Dim dbs As Database

Set dbs = CurrentDb
num = Nr_Motores.Value
nrof = PF.Value


    ' Create a table with three fields and a primary
    ' key.
    dbs.Execute "CREATE TABLE NewTable " _
        & "(FirstName CHAR, LastName CHAR, " _
        & "SSN INTEGER CONSTRAINT MyFieldConstraint " _
        & "PRIMARY KEY);"
End Sub

The CREATE TABLE part is copy/pasted from the documentation, no changes were made yet, I don't know how to include that variable in the name of the table

Upvotes: 0

Views: 1310

Answers (1)

Alex K.
Alex K.

Reputation: 175768

Build the string concatenating the variable with &.

Enclose the name in [] to allow it to contain characters that would otherwise fail to parse.

dbs.Execute "CREATE TABLE [" & nrof & "] _
    & "(FirstName CHAR, LastName CHAR, " _
    & "SSN INTEGER CONSTRAINT MyFieldConstraint " _
    & "PRIMARY KEY);"

Upvotes: 1

Related Questions