473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to programmatically create a DB and a table and link it to another

8,435 Expert 8TB
Hi all.

Ok, I'm using VB6 but I think the answer to this (if there is one) is more likely to be found in the Access forum.

I have a situation where I've got tens of millions of records, spread over a bunch of separate databases. I don't have a choice about this, as the data is simply too large for a single MDB. As yet, I'm not prepared to go to SQL Server. Possibly in the not-too-distant future, but we'll see. In the meantime, I have this bunch of large-ish databases (say, half a gigabyte each) all with the same single table (identical structure). I then have a front-end database which has all of them linked, and also has a query definition related to each one (to join them up with one or two lookup tables).

I use a VB6 program to read some regularly-generated log files and produce information in a condensed form. I then use a VBA routine to import the info to the latest table, update some stats and so forth. (The stats can be generated on the fly from the data, but I don't usually want to wait a day or two for them).

The problem is that as more data is recorded, I have to manually create the next database, with its empty table, then link it to the front-end, then create the query for it, and so on. I'm looking to automate this, and just for good measure I plan to combine the two steps into a single VB6 program which reads the logs and loads the info directly into the database, instead of producing a text file to be imported.

So, can anyone help me on how, from VB6, I can...
  1. Create an MDB containing an empty table
  2. Link the table to my front-end database, and
  3. (Probably the easy one) create a new query definition based on the linked table?

I'm fine with all the logic behind this, by the way - I can build the application no worries. I just need help on how to achieve these specific tasks. Have tried searching, and the best I found was a hint on how to create a table in an existing database.

I suppose I can achieve task #1 relatively simply by just copying a "stock" MDB and changing the name, but would prefer to create it from scratch. Task #3 is probably not too tough - I expect I can figure that one out with a bit of work. But #2 has me stumped.
Jul 24 '07 #1
15 7704
Lysander
344 Expert 100+
Hi all.

Ok, I'm using VB6 but I think the answer to this (if there is one) is more likely to be found in the Access forum.

I have a situation where I've got tens of millions of records, spread over a bunch of separate databases. I don't have a choice about this, as the data is simply too large for a single MDB. As yet, I'm not prepared to go to SQL Server. Possibly in the not-too-distant future, but we'll see. In the meantime, I have this bunch of large-ish databases (say, half a gigabyte each) all with the same single table (identical structure). I then have a front-end database which has all of them linked, and also has a query definition related to each one (to join them up with one or two lookup tables).

I use a VB6 program to read some regularly-generated log files and produce information in a condensed form. I then use a VBA routine to import the info to the latest table, update some stats and so forth. (The stats can be generated on the fly from the data, but I don't usually want to wait a day or two for them).

The problem is that as more data is recorded, I have to manually create the next database, with its empty table, then link it to the front-end, then create the query for it, and so on. I'm looking to automate this, and just for good measure I plan to combine the two steps into a single VB6 program which reads the logs and loads the info directly into the database, instead of producing a text file to be imported.

So, can anyone help me on how, from VB6, I can...
  1. Create an MDB containing an empty table
  2. Link the table to my front-end database, and
  3. (Probably the easy one) create a new query definition based on the linked table?

I'm fine with all the logic behind this, by the way - I can build the application no worries. I just need help on how to achieve these specific tasks. Have tried searching, and the best I found was a hint on how to create a table in an existing database.

I suppose I can achieve task #1 relatively simply by just copying a "stock" MDB and changing the name, but would prefer to create it from scratch. Task #3 is probably not too tough - I expect I can figure that one out with a bit of work. But #2 has me stumped.
I used to do this all the time, but that was in Access 2 and I no longer have a copy of my solution, nor can I remember how I did it. However, I did a search and this article explains how to do step 2, and provides the code you need.
http://www.alvechurchdata.co.uk/acclink.htm
Jul 24 '07 #2
Killer42
8,435 Expert 8TB
... this article explains how to do step 2, and provides the code you need.
http://www.alvechurchdata.co.uk/acclink.htm
Thanks, I'll check it out.

One thing I've been wondering is whether I might need to add a reference to the Access object library, then play with Access the way we use Word or Excel. I'm guessing ADO doesn't want to know about the mechanics of Access, since it's a more generic "any database" connection.

Anyway, I'm rambling. I'll go have a look at the link.
Jul 24 '07 #3
FishVal
2,653 Expert 2GB
Thanks, I'll check it out.

One thing I've been wondering is whether I might need to add a reference to the Access object library, then play with Access the way we use Word or Excel. I'm guessing ADO doesn't want to know about the mechanics of Access, since it's a more generic "any database" connection.

Anyway, I'm rambling. I'll go have a look at the link.
Hi!

Here is very basic code creating new mdb, adding to it a simple table and linking it to the current db.
Expand|Select|Wrap|Line Numbers
  1. Public Sub CreateNewMDB()
  2.  
  3.     Dim appAccess As Access.Application
  4.     Dim tdfNewTable As TableDef
  5.  
  6.     Set appAccess = CreateObject("Access.Application")
  7.  
  8.     With appAccess
  9.         '.Visible = True
  10.         .NewCurrentDatabase "F:\Access\Samples\New MDBs\db1.mdb"
  11.         Set tdfNewTable = .CurrentDb.CreateTableDef("tbl")
  12.         With tdfNewTable
  13.             Debug.Print .Updatable
  14.             .Fields.Append .CreateField(Name:="keyID", Type:=dbLong)
  15.             .Fields.Append .CreateField(Name:="txtField", Type:=dbText)
  16.         End With
  17.         .CurrentDb.TableDefs.Append tdfNewTable
  18.         .CloseCurrentDatabase
  19.         .Quit acQuitSaveAll
  20.     End With
  21.     Set appAccess = Nothing
  22.  
  23.     With CurrentDb
  24.         Set tdfNewTable = .CreateTableDef("tblLinked", dbAttachExclusive, "tbl", _
  25.                           ";DATABASE=F:\Access\Samples\New MDBs\db1.mdb")
  26.         .TableDefs.Append tdfNewTable
  27.     End With
  28.  
  29.     Set tdfNewTable = Nothing
  30.  
  31. End Sub
  32.  
BTW Access object library is referenced by default.

Good luck.
Jul 24 '07 #4
Killer42
8,435 Expert 8TB
Here is very basic code creating new mdb, adding to it a simple table and linking it to the current db. ...
Thanks! All contributions welcome. :)

BTW Access object library is referenced by default.
In VB6? Are you sure of that? The reason I thought about using it is that I noticed it, unchecked, on the list of available libraries.

Hang on, I'll go check...

Nope. I'll have to conclude that you're referring to VBA.
Jul 24 '07 #5
FishVal
2,653 Expert 2GB
Thanks! All contributions welcome. :)

In VB6? Are you sure of that? The reason I thought about using it is that I noticed it, unchecked, on the list of available libraries.

Hang on, I'll go check...

Nope. I'll have to conclude that you're referring to VBA.
Sure this is VBA. Sorry, didn't pay attention you want it in VB6.
Jul 24 '07 #6
Killer42
8,435 Expert 8TB
... Here is very basic code creating new mdb, adding to it a simple table and linking it to the current db...
Thanks for that. I've been playing with it have managed to create my Mdb, add the table and add my fields and indexes. What I can't work out so far is how to turn on Unicode compression for a field. Any ideas?

My databases are going to be pretty big, and I don't want to make them even bigger by leaving this option turned off. But since I can't find anything in the DAO documentation, I'm guessing it's Access-specific and DAO doesn't know about it? Or perhaps I need to put a particular value in .Attributes for the field or something?
Jul 26 '07 #7
FishVal
2,653 Expert 2GB
Thanks for that. I've been playing with it have managed to create my Mdb, add the table and add my fields and indexes. What I can't work out so far is how to turn on Unicode compression for a field. Any ideas?

My databases are going to be pretty big, and I don't want to make them even bigger by leaving this option turned off. But since I can't find anything in the DAO documentation, I'm guessing it's Access-specific and DAO doesn't know about it? Or perhaps I need to put a particular value in .Attributes for the field or something?
It is in Field.Properties collection. As well as almost all other field properties available in table design view.
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.TableDefs("tbl").Fields("txtText").Properties("UnicodeCompression").Value = True
  2.  
Jul 26 '07 #8
Killer42
8,435 Expert 8TB
It is in Field.Properties collection. As well as almost all other field properties available in table design view.
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.TableDefs("tbl").Fields("txtText").Properties("UnicodeCompression").Value = True
Guess I didn't quite get it right. Most likely I'm referencing the wrong "field" type object or something. I've got ADO and DAO floating around, plus the Access object library - it's getting confusing. Anyway, I get "Property not found".

I didn't follow your suggestion exactly - I've already got the tabledef and fielddef there, so didn't feel it necessary to use the longer references. Anyway, details below.

Hope you can help a little further on this. Based on your example, I originally had this line, which works...
Expand|Select|Wrap|Line Numbers
  1. .Fields.Append .CreateField("MyField", dbText, 20)
In order to try and set the Unicode compression, I created a new temp object and used it as follows...
Expand|Select|Wrap|Line Numbers
  1. Dim TempField As DAO.Field
  2.  
  3. Set TempField = .CreateField("MyField", dbText, 20)
  4. ' The following line produces error "Property Not Found"...
  5. TempField.Properties("UnicodeCompression").Value = True
  6. .Fields.Append TempField
Jul 27 '07 #9
Killer42
8,435 Expert 8TB
Oh, one more question. Everything apart from the Unicode compression seems to be working (and I can probably live with that). But how can I stop Access from flashing up on the screen each time I call my create-a-table routine?

This is not critical. It's only visible very briefly, and this will only happen once every few months. But it's really annoying - especially during testing, which has to create a whole bunch.
Jul 27 '07 #10
rewalk
17
In VBA it's DoCmd.SetWarnings False.
Jul 27 '07 #11
FishVal
2,653 Expert 2GB
Guess I didn't quite get it right. Most likely I'm referencing the wrong "field" type object or something. I've got ADO and DAO floating around, plus the Access object library - it's getting confusing. Anyway, I get "Property not found".

I didn't follow your suggestion exactly - I've already got the tabledef and fielddef there, so didn't feel it necessary to use the longer references. Anyway, details below.

Hope you can help a little further on this. Based on your example, I originally had this line, which works...
Expand|Select|Wrap|Line Numbers
  1. .Fields.Append .CreateField("MyField", dbText, 20)
In order to try and set the Unicode compression, I created a new temp object and used it as follows...
Expand|Select|Wrap|Line Numbers
  1. Dim TempField As DAO.Field
  2.  
  3. Set TempField = .CreateField("MyField", dbText, 20)
  4. ' The following line produces error "Property Not Found"...
  5. TempField.Properties("UnicodeCompression").Value = True
  6. .Fields.Append TempField
Hi.

I've checked it and here are results.
  • Not all properties are automatically created when Field object is created (20 properties for Field (text type) created programmatically vs 27 for that one created in table design view), "UnicodeCompression" among them
  • CreateProperty method may be used to create property
  • Properties.Append method works only on Field which is "in a Microsoft Jet workspace" only (Access help says), so TableDef object has to be appended to TableDefs collection before Property will be appended to Field.Properties collection

So the code is the following:
Expand|Select|Wrap|Line Numbers
  1.     ....................
  2.     Dim proNewProperty As DAO.Property
  3.     ................
  4.     [Database object].TableDefs.Append tdfNewTable
  5.     Set proNewProperty = [Database object].CreateProperty(Name:="UnicodeCompression", _
  6.         Value:=True, Type:=dbBoolean)
  7.     [Database object].TableDefs("tbl").Fields("txtField").Properties.Append proNewProperty
  8.     ..................
  9.  
Jul 27 '07 #12
FishVal
2,653 Expert 2GB
Oh, one more question. Everything apart from the Unicode compression seems to be working (and I can probably live with that). But how can I stop Access from flashing up on the screen each time I call my create-a-table routine?

This is not critical. It's only visible very briefly, and this will only happen once every few months. But it's really annoying - especially during testing, which has to create a whole bunch.
So far I have completely no idea how to fix it. Plz let me know if you'll find a solution. ;)
Jul 27 '07 #13
Killer42
8,435 Expert 8TB
So far I have completely no idea how to fix it. Plz let me know if you'll find a solution. ;)
It's likely to be a while before I have a chance to look for one, but I'll keep you in mind if/when I do.

As for the property stuff, I'll give it a try. Thanks for all your help. (This reminds me of why I came to TheScripts in the first place.)
Jul 28 '07 #14
Killer42
8,435 Expert 8TB
In VBA it's DoCmd.SetWarnings False.
Thanks for that. Unfortunately I'm working in VB6, so I don't know how (or whether) I can make use of this. Will let everyone know if I find out.
Jul 29 '07 #15
Killer42
8,435 Expert 8TB
I have a few things to report.
  1. I managed to get the Unicode compress property added and working. Thanks for the info.
    It took a while, as I kept running into silly little non-intuitive things that you apparently just have to know. Like the fact that if I create a property, then append it to a fields Properties collection, I have to create it again before I can append it to another.
  2. In case anyone is interested, it's upon executing the .Quit acQuitSaveAll that the Access window flashes briefly but annoyingly on the screen.
  3. For some reason, when I add the "unicodecompression" property, I no longer get the Access window flashing briefly on the screen. I've tried adding and removing the code, and this seems to be what makes the difference.
  4. Also for unknown reasons, I cannot use a For Each loop to go through the fields in the tabledef to add the new property.
    Let's say I start out with this...
    Expand|Select|Wrap|Line Numbers
    1. Set prpNewProperty = .CurrentDb.CreateProperty("UnicodeCompression", dbBoolean, True)
    This works...
    Expand|Select|Wrap|Line Numbers
    1.     .CurrentDb.TableDefs(TempTableName).Fields("Field1").Properties.Append prpNewProperty
    But if I try this...
    Expand|Select|Wrap|Line Numbers
    1. For Each fldNewField In .CurrentDb.TableDefs(TempTableName).Fields
    2.   With fldNewField
    3.     If .Type = dbText Then
    4.       .Properties.Append prpNewProperty
    5.     End If
    6.   End With
    7. Next
    8.  
    it fails on the For Each, with Object invalid or no longer set. I believe this is not due to trying to use prpNewProperty more than once, as it bombs the instant it hits the For Each.
Jul 31 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: ALthePal | last post by:
Hi, I'm not sure if we are able to or even how to loop through the web forms in a VB.NET project during design time. In MSAccess we are able to go through the database -> forms collection and...
2
by: Prakash Wadhwani | last post by:
I have an Access Database called "TEMP" On Opening the "TEMP" Database, I need to use VBA (programmatically): a) to connect to another database called "PMF" b) the PMF database requires a...
11
by: Zlatko Matić | last post by:
Hello. I have a MS Access front-end working with PostgreSQL database. I have successfully created saved File DSN. My paa-through queries are referring to that file as well as linked tables. But I...
6
by: Null Reference | last post by:
Anybody here who can explain or point me to a link ? I wish to create a blank MS Access DB file programmatically using C# . Thanks, nfs
5
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
14
by: mistral | last post by:
Need php script to create mySQL database programmatically; since hosting configuration may not allow create database from script, script also need eliminate/rewrite all restrictions in appropriate...
25
by: bubbles | last post by:
Using Access 2003 front-end, with SQL Server 2005 backend. I need to make the front-end application automatically refresh the linked SQL Server tables. New tables will be added dynamically in...
4
by: alun65 | last post by:
I'm attempting to programmatically build up some HTML in the code behind. Like so: // Create Hyperlink HyperLink link = new HyperLink(); link.NavigateUrl = "nice cat"; link.Text = "Cats...
11
by: =?Utf-8?B?UGV0ZXIgSw==?= | last post by:
I am working with Visual Studio or alternately with Expression Web. I need to create about 50 aspx pages with about 1200 thumbnali images, typically arranged in three to four groups per page,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.