mrmmickle1
Well-known Member
- Joined
- May 11, 2012
- Messages
- 2,461
I am trying to create multiple tables that all have the same fields. I have tried to use an array but it seems like it won't accept it this way. My code looks like this:
I know that the error has to do with the array because when I change the SQL Statement to this it will execute fine:
Can I not use an array? Do I have do make the tables all separately???
Code:
[COLOR=#0000ff]Dim[/COLOR] i [COLOR=#0000ff]As Long[/COLOR]
[COLOR=#0000ff]Dim [/COLOR]TableArr [COLOR=#0000ff]As Variant[/COLOR]
TableArr = Array("All", "Genetics", "Reprep", "RapidFire", "Quest_SendOut", "Needs_Screening", "Needs_Data", "Clerical_Review", "Compliance", "Other")
[COLOR=#0000ff] Set [/COLOR]cnt = CreateObject("ADODB.Connection")
[COLOR=#0000ff] With[/COLOR] cnt
[COLOR=#008000] 'Creating connection with database[/COLOR]
.Open dbConnectStr
[COLOR=#008000] 'Create All Tables[/COLOR]
[COLOR=#0000ff] For [/COLOR]i =[COLOR=#0000ff] LBound[/COLOR](TableArr)[COLOR=#0000ff] To[/COLOR] [COLOR=#0000ff]UBound[/COLOR](TableArr)
strSql = "CREATE TABLE " & TableArr(i) & " ([MR_Num] TEXT(20)"
strSql = strSql & vbLf & ", [Chart_Number] TEXT(12)"
strSql = strSql & vbLf & ", [Last_Name] TEXT(25)"
strSql = strSql & vbLf & ", [First_Name] TEXT(25)"
strSql = strSql & vbLf & ", [Date_Received] DATETIME"
strSql = strSql & vbLf & ", [Sales_Rep] TEXT(10)"
strSql = strSql & vbLf & ", [Hold_Reason] TEXT(200));"
[COLOR=#ff0000] .Execute strSql 'I get a Syntax error in CREATE TABLE statement Error Message on this line[/COLOR]
[COLOR=#0000ff] Next [/COLOR]i
.Close
[COLOR=#0000ff] End With[/COLOR]
I know that the error has to do with the array because when I change the SQL Statement to this it will execute fine:
Code:
[COLOR=#0000ff]With [/COLOR]cnt
.Open dbConnectStr
strSql = "CREATE TABLE TEST_TABLE ([MR_Num] TEXT(20)"
strSql = strSql & vbLf & ", [Chart_Number] TEXT(12)"
strSql = strSql & vbLf & ", [Last_Name] TEXT(25)"
strSql = strSql & vbLf & ", [First_Name] TEXT(25)"
strSql = strSql & vbLf & ", [Date_Received] DATETIME"
strSql = strSql & vbLf & ", [Sales_Rep] TEXT(10)"
strSql = strSql & vbLf & ", [Hold_Reason] TEXT(200));"
.Execute strSql
.Close
[COLOR=#0000ff] End With[/COLOR]