Results 1 to 14 of 14

Thread: [RESOLVED] Creating db Table during runtime

  1. #1

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Resolved [RESOLVED] Creating db Table during runtime

    I have this code and it will works like this

    the Set db1 = OpenDatabase("C:\temp.mdb")

    Code:
    On Error GoTo errorhandler
    
    Set db1 = OpenDatabase("C:\temp.mdb")
    Set td = db1.TableDefs("SubBrand")
    
    On Error Resume Next
    Set f1 = td.Fields(strString)
        
        If Err.Number = 0 Then
            MsgBox "Exists"
        Else
            Set td = db1.TableDefs("SubBrand")
            
            Set fl = td.CreateField(strString, dbText)
            td.Fields.Append fl
        End If
    
    Exit Sub
    
    errorhandler:
    
    If Not db1 Is Nothing Then db1.Close
    Err.Raise Err.Number
    Exit Sub

    But not like this

    Code:
    On Error GoTo errorhandler
    
    Set db1 = OpenDatabase("F:\VB\Baseball\database.mdb")
    Set td = db1.TableDefs("SubBrand")
    
    On Error Resume Next
    Set f1 = td.Fields(strString)
        
        If Err.Number = 0 Then
            MsgBox "Exists"
        Else
            Set td = db1.TableDefs("SubBrand")
            
            Set fl = td.CreateField(strString, dbText)
            td.Fields.Append fl
        End If
    
    Exit Sub
    
    errorhandler:
    
    If Not db1 Is Nothing Then db1.Close
    Err.Raise Err.Number
    Exit Sub
    or like this

    Code:
    On Error GoTo errorhandler
    
    Set db1 = OpenDatabase(app.path & "\database.mdb")
    Set td = db1.TableDefs("SubBrand")
    
    On Error Resume Next
    Set f1 = td.Fields(strString)
        
        If Err.Number = 0 Then
            MsgBox "Exists"
        Else
            Set td = db1.TableDefs("SubBrand")
            
            Set fl = td.CreateField(strString, dbText)
            td.Fields.Append fl
        End If
    
    Exit Sub
    
    errorhandler:
    
    If Not db1 Is Nothing Then db1.Close
    Err.Raise Err.Number
    Exit Sub
    can somebody tell me why this won't work with my database and does with the temp database that I created when testing my code.

    by the way F: is an external HD if anyone is wondering

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating db Table during runtime

    So you are doing this in Access VBA or VB6 with an Access reference?

    What OS are you using? Do you have permissions to the folders? If Vista then the file locations may be Virtualized. what is the error message you are getting?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating db Table during runtime

    Are you trying to open a database or create one. If it is creating as the title suggests then check the link in my signature...

    Regarding this

    vb Code:
    1. Set db1 = OpenDatabase("F:\VB\Baseball\database.mdb")

    Include this one line and tell me what do you get?

    vb Code:
    1. Msgbox DoesItExist("F:\VB\Baseball\database.mdb")

    But before you do that paste the below code in a module...

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, _
    4. lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    5.  
    6. Private Type OFSTRUCT
    7.     cBytes As Byte
    8.     fFixedDisk As Byte
    9.     nErrCode As Integer
    10.     Reserved1 As Integer
    11.     Reserved2 As Integer
    12.     szPathName(OFS_MAXPATHNAME) As Byte
    13. End Type
    14.  
    15. Private Const OF_EXIST         As Long = &H4000
    16. Private Const OFS_MAXPATHNAME  As Long = 128
    17. Private Const HFILE_ERROR      As Long = -1
    18.      
    19. Public Function DoesItExist(ByVal strFileName As String) As Boolean
    20.     Dim lngRet As Long, Ofs As OFSTRUCT
    21.    
    22.     lngRet = OpenFile(strFileName, Ofs, OF_EXIST)
    23.     If lngRet <> HFILE_ERROR Then
    24.         DoesItExist = True
    25.     Else
    26.         DoesItExist = False
    27.     End If
    28. End Function

    And Regarding this

    Set db1 = OpenDatabase(app.path & "\database.mdb")
    You can either try what I mentioned above or include this one line before the above and tell me what do you get?

    vb Code:
    1. Msgbox app.path


    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating db Table during runtime

    lol now where have I seen that FileExists code before
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating db Table during runtime

    Probably here?

    http://www.vbforums.com/showthread.php?t=562355

    This is the code that I use frequently and I have saved it in my Code Generator (In my link)... I did search the forums and got 379 links. Which link are you referring to?

    http://www.vbforums.com/search.php?searchid=2234338
    Last edited by Siddharth Rout; Sep 20th, 2009 at 04:06 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating db Table during runtime

    May 22nd, 2006 http://vbforums.com/showpost.php?p=2479326&postcount=2



    But lets not clog up the thread with this. We still need to know some answers to our questions on the original issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: Creating db Table during runtime

    RobDog888:
    I believe VB6 w/ access reference, I've done some db programming but still don't know all the terms.
    OS is XP
    and don't know about permission to folders. It is the same database in c:\temp.mdb as in F:\VB\Baseball\database.mdb - just moved it and renamed.
    if you mean permission to f: folders then yes I do.

    Koolsid:
    Tried your code, I get an error here
    szPathName(OFS_MAXPATHNAME) As Byte
    compile error
    constant expression required

  8. #8

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: Creating db Table during runtime

    Also, I want to create a new field in an existing table sorry about that.

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Creating db Table during runtime

    If the database has a connection in the application already then you use the alter table SQL command and execute it on the connection to the database using a command object

    Alter table tablename add column columnname datatype
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating db Table during runtime

    Ok try this in the code that I gave above...

    Move line number 15,16,17 in the code above to line number 5 i.e before

    Private Type OFSTRUCT

    and now try it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: Creating db Table during runtime

    Ok Koolsid, I looked at your Creating Access Database via Code and I finally got it to add a field to the database, but how do I have it check if the field already exists in the table.

    Heres the code I have
    Code:
        Dim dbDatabase As Database
        Dim td As TableDef
        Dim f1 As Field
    
        db.Close
        Set dbDatabase = Workspaces(0).OpenDatabase(App.Path & "\database.mdb")
        
        Set td = dbDatabase.TableDefs("SubBrand")
        
        Set f1 = td.CreateField("txtField1", dbText)
        td.Fields.Append f1
        dbDatabase.Close
        Set db = OpenDatabase(DBPath)
    
        
        MsgBox "New Field Created - '" & sNewDBPathAndName & "'", vbInformation

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating db Table during runtime

    Try this function...

    vb Code:
    1. Function FieldExists(ByVal fieldName As String, ByVal tableName As String) As Boolean
    2.     Dim dbDatabase As Database, td As TableDef, f1 As Field
    3.    
    4.     Set dbDatabase = CurrentDb
    5.     Set td = dbDatabase.TableDefs(tableName)
    6.    
    7.     For Each f1 In td.Fields
    8.         If f1.Name = fieldName Then
    9.             FieldExists = True
    10.             Exit For
    11.         End If
    12.     Next
    13. End Function
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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

    Re: Creating db Table during runtime

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

  14. #14

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: Creating db Table during runtime

    Thank you for your help KoolSid

    I finally got it working.

    Thanks again
    jlt7

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