|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
VB programming for database application
I am developing a program in VB5 to work with an MS Access database. I have written code for to check if the file exists and to create the database file if it does not exist. Now I have a problem to solve.
The database contains several related tables. Some tables are created at the time of creating the database. Some tables have to be created during run time if they do not already exist. For this I have to ascertain whether the database already has the table. If the table is not there I have to create it. I can write the code for creating the table. But how do I find out whether the table is already present in the database? What code I should include to do this? Thanks in advance - Anant |
|
#2
|
||||
|
||||
|
If you are using ms sql, you can use the sysobjects table to check if the table existes
Code:
sqlstr = "select name from sysobjects where name = 'tableName'" rs.open sqlstr, conn if rs.eof then 'code to create table end if or (for any database) you can just route the error to a table create area, while selecting stuff from the table (I don't know if this is cheating) Code:
on error goto createTable sqlstr = "select top 1 * from tableName" rs.open sqlstr, conn 'the rest of the code exit sub createTable: 'code to create table resume next I know that there is a better way to do this, but I can't remember how, so I thought that I should post this until I (or someone else) finds it. |
|
#3
|
|||
|
|||
|
VB programming for database application
Thanks Silian.
But I am not using MS SQL. I am writing only in VB5 code. I tried the following code and it worked. Give your valuable comments. ... ... Dim dbs As Database Dim dbFile As String, Tbl As String Dim FoundTbl As Boolean Dim X dbFile = "MyVBProject.mdb" Tbl = "SampleTable" Set dbs = OpenDatabase(dbFile) FoundTbl = False With dbs For Each X In .TableDefs If X.Name = Tbl Then FoundTbl=True End If Next End With dbs.close If FoundTbl = False Then Call CreateTable End If ... ... - Anant Navale |
|
#4
|
|||
|
|||
|
Well, in Access it is MSysObjects that contains the table names
sqlstr = "select * from MSysObjects where name = 'tableName'" rs.open sqlstr, conn if rs.eof then 'code to create table end if |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > VB programming for database application |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|