Results 1 to 9 of 9

Thread: [RESOLVED] Creating Access database, and tables at runtime

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Resolved [RESOLVED] Creating Access database, and tables at runtime

    am making an app which uses a access database, but if the current database becomes corrupt or unreadable, because the user messing with the format of the database...

    How would i make a Database in VB, with tables and relationships.

    Or is that going to be difficult?

    Any help appreciated
    ilmVBBBBBBBBB

    '

    Post No. 300
    Last edited by I_Love_My_Vans; Aug 18th, 2005 at 12:11 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating Access database, and tables at runtime

    In SQL there is CREATE DATABASE...however, to run that from VB, you would need to be connected to a database, so that isn't going to buy you anything.

    I don't think you can create the database, but you certainly could create the tables. If you are worried about database corruption, then maybe you should create and empty database, and ship it with your program.

    You can also create indexs and primary keys.

    The only thing I don't think you would be able to do is create the database itself.

  3. #3
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Creating Access database, and tables at runtime

    Go to
    http://www.4guysfromrolla.com/webtech/013101-1.shtml
    Works like a charm.
    Cut-n-paste the examples there.
    You need to add the reference to "Microsoft ADO ext. ..."
    and remove the "Server." from the CreateObject (that's for ASP)

    I don't know about the relationships though.

    have fun, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Creating Access database, and tables at runtime

    You could also just keep a "golden" copy somewhere, and when the live copy gets corrupted just copy the golden one over it.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating Access database, and tables at runtime

    Ahhhhhhhhhh...you use the Catelog object to create the database, and then just standard SQL to create the tables and whatnot.

    Very Cool.

  6. #6
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Creating Access database, and tables at runtime

    You can also use ".tables.append" to create tables on the fly..Here's an example of something I'm using.

    VB Code:
    1. Option Compare Text
    2. Private catMake As ADOX.Catalog
    3. Private tblMake As ADOX.Table
    4.  
    5. Public Function createDb(Optional Jetv3xNot4 As Boolean = True) As Boolean
    6.     On Error GoTo Err_Hand
    7.  
    8.     Dim sPath As String
    9.     createDb = False
    10.     sPath = "path\dbName.mdb"
    11.    
    12.     dbConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    13.         "Data Source=" & sPath
    14.    
    15.    
    16.     If Jetv3xNot4 Then
    17.         dbConnStr = dbConnStr & ";Jet OLEDB:Engine Type=4"
    18.     End If
    19.  
    20.     Set catMake = New ADOX.Catalog
    21.     catMake.Create dbConnStr
    22.  
    23.     If MadetblList = False Then
    24.         GoTo CleanUp
    25.     End If
    26.  
    27.     createDb = True
    28.  
    29.  
    30. CleanUp:
    31.    Set catMake = Nothing
    32.    
    33.    Exit Function
    34.  
    35. Err_Hand:
    36. MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf & "Source: MadeNewADOJetDB"
    37. Err.Clear
    38. GoTo CleanUp
    39.  
    40. End Function
    41.  
    42. Private Function MadetblList() As Boolean
    43.     On Error GoTo Err_Hand
    44.  
    45.     Dim colMake As Column
    46.     Dim idxMake As Index
    47.     Set tblMake = New Table
    48.  
    49.     With tblMake
    50.         .name = "tblList"
    51.         .ParentCatalog = catMake
    52.  
    53.         Set colMake = New Column
    54.         colMake.name = "Email"
    55.         colMake.Type = adVarWChar
    56.         .Columns.Append colMake
    57.         colMake.DefinedSize = 255
    58.         colMake.Properties("Autoincrement") = False
    59.         colMake.Properties("Nullable") = True
    60.         colMake.Properties("Fixed Length") = False
    61.         colMake.Properties("Jet OLEDB:IISAM Not Last Column") = False
    62.         colMake.Properties("Jet OLEDB:AutoGenerate") = False
    63.         colMake.Properties("Jet OLEDB:One BLOB per Page") = False
    64.         colMake.Properties("Jet OLEDB:Compressed UNICODE Strings") = True
    65.         colMake.Properties("Jet OLEDB:Allow Zero Length") = True
    66.         colMake.Properties("Jet OLEDB:Hyperlink") = False
    67.        
    68.         Set colMake = New Column
    69.         colMake.name = "allowDeny"
    70.         colMake.Type = adVarWChar
    71.         .Columns.Append colMake
    72.         colMake.DefinedSize = 25
    73.         colMake.Properties("Autoincrement") = False
    74.         colMake.Properties("Nullable") = False
    75.         colMake.Properties("Fixed Length") = False
    76.         colMake.Properties("Jet OLEDB:IISAM Not Last Column") = False
    77.         colMake.Properties("Jet OLEDB:AutoGenerate") = False
    78.         colMake.Properties("Jet OLEDB:One BLOB per Page") = False
    79.         colMake.Properties("Jet OLEDB:Compressed UNICODE Strings") = True
    80.         colMake.Properties("Jet OLEDB:Allow Zero Length") = True
    81.         colMake.Properties("Jet OLEDB:Hyperlink") = False
    82.        
    83.         Set colMake = New Column
    84.         colMake.name = "ID"
    85.         colMake.Type = adInteger
    86.         .Columns.Append colMake
    87.         colMake.DefinedSize = 10
    88.         colMake.Precision = 10
    89.         colMake.Properties("Autoincrement") = True
    90.         colMake.Properties("Nullable") = True
    91.         colMake.Properties("Fixed Length") = True
    92.         colMake.Properties("Jet OLEDB:IISAM Not Last Column") = False
    93.         colMake.Properties("Jet OLEDB:AutoGenerate") = False
    94.         colMake.Properties("Jet OLEDB:One BLOB per Page") = False
    95.         colMake.Properties("Jet OLEDB:Compressed UNICODE Strings") = False
    96.         colMake.Properties("Jet OLEDB:Allow Zero Length") = False
    97.         colMake.Properties("Jet OLEDB:Hyperlink") = False
    98.        
    99.         Set idxMake = New Index
    100.         idxMake.name = "PrimaryKey"
    101.         idxMake.Clustered = False
    102.         idxMake.IndexNulls = adIndexNullsDisallow
    103.         idxMake.PrimaryKey = True
    104.         idxMake.Unique = True
    105.         Set colMake = New Column
    106.         colMake.name = "ID"
    107.         colMake.SortOrder = adSortAscending
    108.         idxMake.Columns.Append colMake
    109.         tblMake.Indexes.Append idxMake
    110.        
    111.         Set idxMake = New Index
    112.         idxMake.name = "UniqueStr"
    113.         idxMake.Clustered = False
    114.         idxMake.IndexNulls = adIndexNullsDisallow
    115.         idxMake.PrimaryKey = False
    116.         idxMake.Unique = True
    117.         Set colMake = New Column
    118.         colMake.name = "Email"
    119.         colMake.SortOrder = adSortAscending
    120.         idxMake.Columns.Append colMake
    121.         tblMake.Indexes.Append idxMake
    122.     End With
    123.  
    124.     catMake.Tables.Append tblMake
    125.  
    126.     Set tblMake = Nothing
    127.     Set idxMake = Nothing
    128.     Set colMake = Nothing
    129.  
    130.     MadetblList = True
    131.  
    132.     Exit Function
    133.  
    134. Err_Hand:
    135.     Set tblMake = Nothing
    136.     Set idxMake = Nothing
    137.     Set colMake = Nothing
    138.  
    139.     MsgBox Err.Number & vbCr & Err.Description & vbCr & "MadetblList"
    140.     Err.Clear
    141. End Function

  7. #7

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Creating Access database, and tables at runtime

    Cheers guys, sounds good. The golden idea sounds good. I am making my app at the moment, so will have to explore routes such as preventing rather then recovering.

    cheers guys,

  8. #8
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: [RESOLVED] Creating Access database, and tables at runtime

    YOu might not still be monitoring this thread but...Jet4 has a "repair" option..I've never used it so I can't say how effective it is but it might be worth trying before you replace a current db with an older version...

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Creating Access database, and tables at runtime

    Quote Originally Posted by I_Love_My_Vans
    am making an app which uses a access database, but if the current database becomes corrupt or unreadable, because the user messing with the format of the database...

    How would i make a Database in VB, with tables and relationships.

    Or is that going to be difficult?

    Any help appreciated
    ilmVBBBBBBBBB

    '

    Post No. 300
    Are you using VB6.0 as your Front-End? How come the user could mess the format of your database? And what's the sense of recreating the database if the data in it is lost? You could just have a back-up copy of your database without any records if you want...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width