Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create For/Do Loop to create

Status
Not open for further replies.

Fred48

Programmer
Feb 23, 2004
62
US
Hi,

I would like to create a table using the For/Do loop because the table name is similar except for the last postion which can contain values 1 to 4. Below is my current code:


strCMD = "CREATE TABLE WEEK1 (" & _
"TGSN varchar (8)," & _
"MEAS_PRD varchar (2)," & _
"[HOUR] int," & _
"MCDA varchar (5)," & _
"OFFDCCS varchar (7)," & _
"BLKG varchar (7))"

cmdDatabase = New OleDbCommand(strCMD, cnn)
cmdDatabase.ExecuteNonQuery()

strCMD = "CREATE UNIQUE INDEX PrimaryKeyWEEK ON WEEK1([HOUR])"
cmdDatabase = New OleDbCommand(strCMD, cnn)
cmdDatabase.ExecuteNonQuery()

I am creating four tables, Week1, Week2, Week3 and Week4. So, instead of having four sets of code I would like to have one and somehow change the week number from static to a variable.

Any one have any idease?
 

How about:
Code:
[blue]For i As Integer = 1 To 4[/blue]
        strCMD = "CREATE TABLE  WEEK[blue]" & i & "[/blue]  (" & _
                "TGSN            varchar (8)," & _
                "MEAS_PRD        varchar (2)," & _
                "[HOUR]          int," & _
                "MCDA            varchar (5)," & _
                "OFFDCCS         varchar (7)," & _
                "BLKG            varchar (7))"

        cmdDatabase = New OleDbCommand(strCMD, cnn)
        cmdDatabase.ExecuteNonQuery()

        strCMD = "CREATE UNIQUE INDEX PrimaryKeyWEEK ON WEEK[blue]" & i & "[/blue]([HOUR])"
        cmdDatabase = New OleDbCommand(strCMD, cnn)
        cmdDatabase.ExecuteNonQuery()[blue]
Next i[/blue]

Have fun.

---- Andy
 
Andy,

Thanks for the replay and I incorporated your changes and I received an complier error. The error states, variable 'i' hides a variable in ean enclosed block. Below is the new code:

For i As Integer = 1 To 4
strCMD = "CREATE TABLE WEEK" & i & " (" & _
"TGSN varchar (8)," & _
"MEAS_PRD varchar (2)," & _
"[HOUR] int," & _
"MCDA varchar (5)," & _
"OFFDCCS varchar (7)," & _
"BLKG varchar (7))"

cmdDatabase = New OleDbCommand(strCMD, cnn)
cmdDatabase.ExecuteNonQuery()

strCMD = "CREATE UNIQUE INDEX PrimaryKeyWEEK ON WEEK" & i & "([HOUR])"
cmdDatabase = New OleDbCommand(strCMD, cnn)
cmdDatabase.ExecuteNonQuery()
Next i
 
Andy,

Ignore my last email, operator error on my part. I had a similar soultion but it did not work. But, your soultion does work.

Thanks again!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top