Results 1 to 7 of 7

Thread: [RESOLVED]Add Field to access Database with code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Resolved [RESOLVED]Add Field to access Database with code

    I need to add some fields through code to an access 2000 database mdb file. Doing it manually with access isn't an option. I looked all over and haven't found anything that can do this I hope it's possible. This will be done in VB6.
    Last edited by seanwpb; Apr 22nd, 2007 at 02:11 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Add Field to access Database with code

    How are you currently working with the database?

    If you are using ADO code (or aren't using anything), I would recommend using ADOX - there are several threads that contain examples, so a search should get good results.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Add Field to access Database with code

    Si,

    Dim tbl As New ADOX.Table
    Dim cat As New ADOX.Catalog
    Dim sSQL$
    Dim cnn As ADODB.Connection
    Dim i%

    Set cnn = New ADODB.Connection
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & App.Path & "\work.mdb;"
    Set cat.ActiveConnection = cnn

    cat.Tables("WorkOrders").Columns.Append "NewField"
    cat.Tables("WorkOrders").Columns.Refresh

    Set cat = Nothing
    Set tbl = Nothing


    this just gives me error "user defined type not defined" I have referenced microsoft activex already.
    what am I missing.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Add Field to access Database with code

    You need another reference too - for ADOX itself. I don't use it much, but I just had a look and I think you want "Microsoft ADO Ext. 2.x for DDL and Security"

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Add Field to access Database with code

    Si,

    that did the trick, that reference is from the file msadox.dll, should I now deploy this file with my setup file?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Add Field to access Database with code

    In a way yes - you need to deploy all files that your program requires (unless you are certain that they will be installed on all client computers already).

    In this case tho, I think that simply installing MDAC (which you should do for ADO anyway) does the job, so you don't need to specifically install this file.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Add Field to access Database with code

    Si,

    Thanks for everything it works great. To anyone else who might be working on the same Here is the entire code that worked for me. I placed the code in the submain and the second time it runs it creates a trapable error. if anyone has any suggestions to make this faster or better please post, but this seems to work very well for me.

    VB Code:
    1. 'create new time in & time out fields in database - second time will create error. will trap at bottom with on error
    2.  
    3. Dim tbl As New ADOX.Table
    4. Dim cat As New ADOX.Catalog
    5. Dim sSQL$
    6. Dim cnn As ADODB.Connection
    7. Dim i%
    8.  
    9.     Set cnn = New ADODB.Connection
    10.     cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    11.              "Data Source=" & App.Path & "\work.mdb;"
    12.     Set cat.ActiveConnection = cnn
    13.  
    14.     cat.Tables("WorkOrders").Columns.Append "Time_In" ' create a new field
    15.     cat.Tables("WorkOrders").Columns.Refresh
    16.     cat.Tables("WorkOrders").Columns.Append "Time_Out"'create another new field
    17.     cat.Tables("WorkOrders").Columns.Refresh
    18.  
    19.     Set cat = Nothing
    20.     Set tbl = Nothing
    21. 'done creating fields
    22.  
    23. 'now lets resize the new fields
    24. 'connect to database
    25. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    26.         "Data Source=" & App.Path & "\work.mdb" & ";" & _
    27.         "Persist Security Info=False"
    28.        
    29.        
    30. Set ADOCn = New ADODB.Connection
    31. ADOCn.ConnectionString = strConnString
    32. ADOCn.Open strConnString
    33. 'the default size when adding a new text field is 255 this changed to 25
    34. strTimeIn = "ALTER TABLE WorkOrders ALTER COLUMN Time_In text(25) "
    35. ADOCn.Execute strTimeIn
    36. strTimeOut = "ALTER TABLE WorkOrders ALTER COLUMN Time_Out text(25) "
    37. ADOCn.Execute strTimeOut
    38.  
    39.  
    40. 'now it needs to close the connection
    41. ADOCn.Close
    42.  
    43. ErrorHandler:   ' Error-handling routine.
    44.    Select Case Err.Number   ' Evaluate error number.
    45.      
    46.                
    47.          Case -2147467259' from second time resizing
    48.          Load Form4 ' my startup form
    49.          resume next
    50.          
    51.          Case -2147217858 ' created from second time you try to add new fields
    52.          Load Form4 ' my startup form
    53.          resume next
    54.          
    55.    End Select
    Last edited by si_the_geek; Feb 25th, 2007 at 01:32 PM. Reason: added vbcode tags

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