Solved

VB6 code to create a db and Table

Posted on 2002-04-16
2
217 Views
Last Modified: 2010-05-02
I need help with this vb6 code. It worked at one time and created a database m086.mdb
However, now it gives me an ISAM error before the create. I can't figure out why.

Also, when it DID create the m086.mdb it would not continue and create a table in the new db. How do I then get the table into the m086.mdb?

Private Sub CreateClass_Click()
'CreateDatabase()
On Error GoTo ErrorCreateDB
Dim Cat As New ADOX.Catalog
Dim Tbl(5) As ADOX.Table
Dim Idx() As ADOX.Index
Dim msgErrR As Integer
Dim sCnn As String
NuevaDB$ = "m086"

sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:reEngine Type=5;" + "Data Source=" + NuevaDB$ + ".mdb"

'error next statement:"can not find installable
'ISAM.
' At one time I did NOT get this error now I do !

Cat.Create sCnn
'what code do I need HERE to create a table with specs below?

Stop
Set Tbl(5) = New ADOX.Table
Stop
Set Tbl(5).ParentCatalog = Cat
Stop
With Tbl(5)

.Name = "ClassList"

.Columns.Append "Password", adVarWChar, 5

.Columns.Append "StudenNumber", adInteger

.Columns("StudenNumber").Properties("AutoIncrement").Value = True

.Columns("StudenNumber").Properties("Nullable").Value = False

.Columns.Append "StudentFirst", adVarWChar, 25

.Columns("StudentFirst").Properties("Nullable").Value = False

.Columns.Append "StudentLast", adVarWChar, 25

.Columns("StudentLast").Properties("Nullable").Value = False

.Columns.Append "TeacherFirst", adVarWChar, 25

.Columns("TeacherFirst").Properties("Nullable").Value = False

.Columns.Append "TeacherLast", adVarWChar, 25

.Columns("TeacherLast").Properties("Nullable").Value = False
End With

Stop
ReDim Idx(1)
Set Idx(0) = New ADOX.Index

Idx(0).Name = "ClassID"

Idx(0).IndexNulls = adIndexNullsAllow

Idx(0).Unique = True

Idx(0).Columns.Append "Password"
Set Idx(1) = New ADOX.Index

Idx(1).Name = "PrimaryKey"

Idx(1).PrimaryKey = True

Idx(1).Unique = True

Idx(1).Columns.Append "StudenNumber"
For i = 0 To UBound(Idx)

Tbl(5).Indexes.Append Idx(i)
Next i
Stop
Cat.Tables.Append Tbl(5)
Stop
Set Cat = Nothing
Exit Sub

ErrorCreateDB:

msgErrR = MsgBox(" Error No. " & Err & " " & vbCrLf & Error, vbCritical + vbAbortRetryIgnore, "Code Gen Error")

Select Case msgErrR

Case Is = vbAbort

If Not (Cat Is Nothing) Then

Set Cat = Nothing

End If

Exit Sub

Case Is = vbRetry

Resume Next

Case Is = vbIgnore

Resume

End Select
End Sub


0
Comment
Question by:mathprof
2 Comments
 
LVL 1

Accepted Solution

by:
riaz9 earned 100 total points
Comment Utility
You can try:

Dim catCatalog As New ADOX.Catalog, tblTable As ADOX.Table, colColumn As ADOX.Column

catCatalog.Create "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & Database

set tblTable = New ADOX.Table
tblTable.Name = "MyTable"

set colColumn = new ADOX.Column
colColumn.Name = "ColumnName"  '**
colColumn.Type = adVarWChar    '**
colColumn.DefinedSize = 50     '**
colColumn.Attributes = adColNullable  '**  - this still does not set Allow Zero Length Attribute to Zero (it only sets Required to False)

tblTable.Columns.Append colColumn   '**

'** repeat the code to add as many columns needed.

'for columns like datetime you don't need DefinedSize

catCatalog.Tables.Append tblTable

set tblTable = nothing: set colColumn = nothing: set catCatalog = nothing


hope this helps,

riaz
0
 

Author Comment

by:mathprof
Comment Utility
Thanks Riaz9. I changed the code (see below) modifying a bit what you suggested.

Mathprof


strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + NuevaDB$ + ".mdb"
    Set catNewDatabase = New ADOX.Catalog
    catNewDatabase.Create strConn
    Set catNewDatabase = Nothing
   
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now