Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 29th, 2003, 12:18 PM
asnavale asnavale is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: India
Posts: 5 asnavale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 30th, 2003, 03:49 AM
Silian's Avatar
Silian Silian is offline
Gogga
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: A long, long time ago, in a galaxy, far, far away...
Posts: 34 Silian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
  #3  
Old October 6th, 2003, 11:27 AM
asnavale asnavale is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: India
Posts: 5 asnavale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old October 15th, 2003, 08:49 AM
m_lazor m_lazor is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Luxembourg
Posts: 156 m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > VB programming for database application


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek