|
-
Sep 20th, 2009, 12:34 PM
#1
Thread Starter
Hyperactive Member
[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
-
Sep 20th, 2009, 02:54 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 20th, 2009, 03:52 PM
#3
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:
Set db1 = OpenDatabase("F:\VB\Baseball\database.mdb")
Include this one line and tell me what do you get?
vb Code:
Msgbox DoesItExist("F:\VB\Baseball\database.mdb")
But before you do that paste the below code in a module...
vb Code:
Option Explicit Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, _ lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End Type Private Const OF_EXIST As Long = &H4000 Private Const OFS_MAXPATHNAME As Long = 128 Private Const HFILE_ERROR As Long = -1 Public Function DoesItExist(ByVal strFileName As String) As Boolean Dim lngRet As Long, Ofs As OFSTRUCT lngRet = OpenFile(strFileName, Ofs, OF_EXIST) If lngRet <> HFILE_ERROR Then DoesItExist = True Else DoesItExist = False End If 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?
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
-
Sep 20th, 2009, 03:55 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 20th, 2009, 04:03 PM
#5
-
Sep 20th, 2009, 04:11 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 22nd, 2009, 11:17 AM
#7
Thread Starter
Hyperactive Member
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
-
Sep 22nd, 2009, 11:22 AM
#8
Thread Starter
Hyperactive Member
Re: Creating db Table during runtime
Also, I want to create a new field in an existing table sorry about that.
-
Sep 22nd, 2009, 11:34 AM
#9
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
-
Sep 22nd, 2009, 12:00 PM
#10
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
-
Sep 22nd, 2009, 12:13 PM
#11
Thread Starter
Hyperactive Member
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
-
Sep 22nd, 2009, 01:19 PM
#12
Re: Creating db Table during runtime
Try this function...
vb Code:
Function FieldExists(ByVal fieldName As String, ByVal tableName As String) As Boolean Dim dbDatabase As Database, td As TableDef, f1 As Field Set dbDatabase = CurrentDb Set td = dbDatabase.TableDefs(tableName) For Each f1 In td.Fields If f1.Name = fieldName Then FieldExists = True Exit For End If Next 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
-
Sep 22nd, 2009, 01:47 PM
#13
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)
-
Sep 22nd, 2009, 09:39 PM
#14
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|