Link to home
Start Free TrialLog in
Avatar of Evlich
Evlich

asked on

Weird Problem with SQL CREATE TABLE?

Hello, I am working on a program that basicly creates a database and I am having problems using the SQL CREATE TABLE to make the tables. The database that I am using is Access (DAO 3.6). Here is a piece of code:
 Set DB = engine.CreateDatabase(txtDatabasePath.Text, dbLangGeneral)
 
  DB.Execute "CREATE TABLE keys (test varchar(10) NOT NULL, ver varchar(1) default 'A', answers varchar(60) NOT NULL)TYPE=MyISAM;", dbFailOnError
  DB.Execute "CREATE TABLE answers (sID varchar(2) NOT NULL, pID varchar(3) NOT NULL, comp_ans varchar(60) default NULL, math_ans varchar(60) default NULL, sci_ans varchar(60) default NULL)TYPE=MyISAM;", dbFailOnError

Is there something wrong that I am missing. I thought for sure that you could do this, but maybe I was mistaken. Thanks a lot for the help.
~evlich
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

The 'TYPE=MyISAM' is not an Access command - it is specific to MySQL.  Try removing that first.

Default is also problematic in Access.  As of Access 97, Default is not accepted as a legal command, but this may have changed in Access 2000.

The default NULL statements can be left off - Access will default to NULL for fields.
Do you have to use DAO?  It's much easier using ADO and ADOX.  I can even supply an example if you want to get out of the dark ages...I mean away from DAO.
Avatar of Evlich
Evlich

ASKER

Well the defaul let me get through those first tow lines, but I have several more line that it is getting trapped on. Can you use tinyint and stuff like that? Here is the new line that the program gets trapped on:
  DB.Execute "CREATE TABLE prog_submissions (tID TINYINT(2) NOT NULL, problem varchar(5) NOT NULL, correct TINYINT(1) NOT NULL, time TIME NOT NULL, er_code tinyint(1));", dbFailOnError
There are still a few lines after that, but I think that this has all the stuff that is used later on. Thanks a lot.
~evlich
P.S. I would appreciate some links to pages for ADO, I have never actually used it, and it might be better for this.
Her's a module to create a DB:
Public Function gfAB_CreateDB(asDB as string) As Boolean
    Dim adoxCat     As ADOX.Catalog
   
    On Error Resume Next
   
    If Len(Dir(asDB)) > 0 Then Kill asDB
   
    On Error GoTo Syntax_Error
   
    Set adoxCat = New ADOX.Catalog
   
    adoxCat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & asDB
   
    Set adoxCat = Nothing
    gfAB_CreateDB = True
   
Exit_Sub:
    Exit Function
   
Syntax_Error:
    gfAB_CreateDB = False
    Resume Exit_Sub

End Function

Here's a function to create a table:
Public Function gfAB_CreateTable(asDB as string) As Boolean
    Dim adoxCat     As New ADOX.Catalog
    Dim adoxTab     As New ADOX.Table
    Dim adoxCol     As ADOX.Column
   
    On Error GoTo Syntax_Error
   
    adoxCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & asDB
   
    adoxTab.Name = "TableNameHere"
   
    Set adoxCol = New ADOX.Column
    adoxCol.Name = "ColNameHere"
    adoxCol.Type = adVarWChar
    adoxCol.DefinedSize = 2
    adoxCol.Attributes = adColNullable
    adoxTab.Columns.Append adoxCol
   
    adoxCat.Tables.Append adoxTab
   
    Set adoxCol = Nothing
    Set adoxTab = Nothing
    Set adoxCat = Nothing
   
    gfAB_CreateTable = True
   
Exit_Sub:
    Exit Function
   
Syntax_Error:
    gfAB_CreateTable = False
    Resume Exit_Sub

End Function


The above would create a table with one varchar(2) column.

Hope that helps.
Steve

PS  Don't forget to add a reference to the ado ext.
Tinyint(2) - I assume that this is a 2 byte integer?

Here are some Access SQL Conversions for you:

4 byte integer, usually INT or INTEGER in most SQL dialects.  Access SQL = INTEGER

2 byte integer, usually smallint in most SQL dialects.  Access SQL = smallint or integer2

1 byte integer, usually tinyint in most SQL dialects.  Access SQL = integer1 or byte



nope it will create a varchar(2)
this will creat an integer column:
    adoxCol.Type = adInteger
Avatar of Evlich

ASKER

Well, that worked, only one more problem, how do you do a date/Time field.
~evlich
Following is a link for creating database through ADOX and learning other features.

http://www.able-consulting.com/ADOX_Faq.htm#Q2

regards,
A. Pandey
I don't know if you are asking me but here's what to use for dates and times:
adoxCol.Type = adDBDate
adoxCol.Type = adDBTime
adoxCol.Type = adDBTimeStamp
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Evlich

ASKER

Thanks a lot. I appreciate all the help and suggestions that I was given by everyone. I will take ADO into consideration.
~evlich