School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,155 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,597 people online right now. Registration is fast and FREE... Join Now!




Creating a Specific Table(If Not present already) In SQL with VB6

 
Reply to this topicStart new topic

> Creating a Specific Table(If Not present already) In SQL with VB6, A nice shortcut when you want to run your database-Related application

Rating  5
Nikhil_07n
Group Icon



post 25 Jan, 2009 - 01:20 AM
Post #1


Introduction : This program uses adodb control, 'On error' checks to create a table when the code is runned.

1) Open connection.

CODE


Set adoconn = New ADODB.Connection
adoconn.Open "Provider=MSDAORA;user id=scott;password=tiger;"
adoconn.CursorLocation = adUseClient



Explanation : Not needed , I think.

2)Main Coding

CODE


Set cmdcrtTable = New ADODB.Command
Set reccrtTable = New ADODB.Recordset

On Error GoTo err1
reccrtTable.Open "email", adoconn, adOpenDynamic, adLockOptimistic, adCmdTable

err1:
cmdcrtTable.CommandText = "Create table Email(fname varchar2(20),lname varchar2(20),uname varchar2(10),pass varchar2(10))"
cmdcrtTable.ActiveConnection = adoconn
On Error GoTo err2
cmdcrtTable.Execute

err2:

'absolutely nothing to do



Explanation :

Lets learn this by taking two cases :

1)The Table 'email' already exists

When the recordset 'reccrtTable' is tried to be opened, it opens without any error and normal executing is done.
the program then , sequentially gets executed, and tries to execute the command 'cmdcrtTable'.........BUT.........as the table already exists, the execution returns an error and the program JUMPS to 'err2' and , eventually, nothing is done and ONLY a recordset is opened.

Result : Without any error reccrtTable gets opened

2)The table 'email' does not exist

When the recordset 'reccrtTable' is tried to be opened, the line returns an ERROR, so the program jumps to 'err1' and without any error the 'cmdcrtTable' gets executed and the table is created.

Result : Without any error cmdcrtTable gets executed and the table 'email' is created.

===============================================

This code is one of my fav.s as its very logical.

I created this after thinking a lot, as i dint knew about any functions for checking whether a table exists or not.

Its specially useful when you want to run your database-linked program on a diff. computer.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

sexy_me502
*



post 30 Sep, 2009 - 07:37 PM
Post #2
> i tried your codes but it doesnt seem to work, maybe i've done something wrong. error message says...

"user-defined type not define"

the error pointing at

Set adoconn = New ADODB.Connection
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:26PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month