473,800 Members | 2,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create DB Example - It works, but is it right?

I am teaching myself vb, and I was wondering if someone can check my code.
-It works, but i was wondering if i was on the right track or if I should
have done it differently.
-Again - thanks in advance to everyone who has helped me so far and will
help me again.

My goal last night was to create an mdb file. ( not through sql server )
just with ado. ( i think i got that terminology right )
-Tonights goal will be to add tables into the mdb file.

Anyway, i followed the example on this link:'
http://www.freevbcode.com/ShowCode.asp?ID=5797

and wrote my own code ( below )

-Here are my questions:
( Keep in mind my basic goal after i teach myself vb is to be able to create
a small vb.exe that will create and use
its own database that will be installed with an install shield - basically a
dummy prog so i can get the feel of a small
dummy application from start to finish. )

In the example, it says to add the Reference from the "COM" tab.
Should I be using something from the .NET tab? What is the difference?

In the Example it said to use the 2.7 ( but I selected 2.8 ) - Again it
worked, but not sure what the difference is.
I am thinking the answer is - no difference?

Any other suggestions ?

Thank You,
Miro

============ Code Example to please check =========

' I also had to add a Reference from the "COM" tab.
'Microsoft ADO Ext. 2.8 for DLL and Security.

' To create the main mdb file. ( Tables will be created in another
function )
Function CreateRequiredD B() As Boolean
Dim lFileExists As Boolean = False

' Without this "If statement" the Exception will catch if the file
already exists.
If Dir$("bla.abc") <> "" Then 'Returns the file name if it exists.
lFileExists = True
Else
Dim catNewDB As New ADOX.Catalog()
Dim sCreateString As String
sCreateString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=bla.abc" ' & DatabaseFullPat h
Try
'Tries to Create the DB
catNewDB.Create (sCreateString)
lFileExists = True 'Created the new DB

Catch e As Exception
'Later write a function to write this to a text error log.
MsgBox("An Exception Occurred - " + e.Message)

lFileExists = False

Finally
catNewDB = Nothing
End Try

End If

Return lFileExists
End Function
May 17 '06 #1
3 1573
Miro,

We have a sample as this as well.
http://www.vb-tips.com/default.aspx?...1-d6dda3c888c8

Included is how to make tables in this sample. It uses only Com for the part
of the creation. If you use a Com, you are not really using Net you use a
DLL which has more chance on a problem than using solely Net. There is in
Net however no method to create an Access Database. If you are learning I
point you on the SQLExpress database. In my opinion is it better to use that
to study.

However I will give some comments on your program inline

Dim lFileExists As Boolean = False

' Without this "If statement" the Exception will catch if the file
already exists.
If Dir$("bla.abc") <> "" Then 'Returns the file name if it exists.
lFileExists = True
Above is created the in Net included standard FileInfo class

Dim fi As New IO.FileInfo("c: \db1.mdb")
If fi.Exists Then

I advice you to use standard methods, which cannot give misunderstandin g
when you are doing maintenance or give the program to others.
Dim sCreateString As String
sCreateString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=bla.abc" ' & DatabaseFullPat h
catNewDB.Create (sCreateString)
A lot of work for a one time operation while it adds nothing (only
misunderstandin gs by those who read your program, what is beneath is at
least as good as)
catNewDB.Create ( "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=bla.abc" ' &
DatabaseFullPat h)

I am not sure if the catNewDb.Create can throw an exception, if that is so
than it is better to use it, however do never this
Catch e As Exception
The "e" is standard used in all methods for events, therefore almost any
word is better than "e". By instance "ex".
'Later write a function to write this to a text error log.
MsgBox("An Exception Occurred - " + e.Message)
Try to avoid the + to concatenate Strings. For that is in VB the &. There
are situations where using the + can go wrong.
lFileExists = False What is the sense of this above, I don't see it
catNewDB = Nothing

Setting something to nothing in VBNet at the end of a method has no sense at
all, than that it cost time. (This is classic VB use)

I hope this gives an idea.

Cor
May 17 '06 #2

This is the sort of review I would give for an internal code review.

Some of the comments are purely stylistic - they are based upon our
coding standards, which although they are basically the MS VB.Net
standards, are not absolute.

Others are functional/design and are more generally applicable.
Formatting/naming et al.
-----------------------------------

1 ) Hungarian Notation has been dropped as a standard - it is
considered cumbersome and not really as useful anymore as the IDE is
more capable (e.g. hovering over a variable will tell you its type)
2) IFileExists - local variables should be camel cased
e.g.
Dim createString as string
[Company Standards]

Unless there is no VB.Net equivalent, the VB6 versions of functionality
should not be used.

e.g.
If you want to check if a file exists, use System.IO.File. Exists()

The reasoning behind this is that as long as people continue to use the
old functionality, they (in general) limit themselves to that
functionality. As mentioned, this is an internal company standard and
the more general population may disagree.

No literal constants within the code body. The db creation string
"Provider=. .." and the "bla.abc" (if required) should be declared as
constants at the top of the routine. It is probably overkill here but
in generally, helps with localization et al. (not, I know a hugh
concern to everyone) and simplifies maintenance a tad as there is no
need to hunt through code.

Functionality/Design
-----------------------------

1) It looks like DatabaseFullPat h is a global scope (or class scope, if
this method is in a class). It should be passed it. The more external
references in a method, the more problematic testing and maintenance
becomes.

1.1) "bla.abc" - what is this? Is it intended to change with each
DatabaseFullPat h ?

2) This can possibly be a sub rather than a function. It depends on
whether or not you care if the db already exists. If you don't care,
then the only issue is when you try to create one and it fails. That
is an exception and should be handled as one rather than with a return
code.

3) This is not a presentation layer function, it shouldn't perform any
screen display - the Msgbox() shouldn't be here.

4) Following on from 3, this is not the best place for catching the
exception. It should be allowed to propagate up.

5) The Finally clause does nothing useful here - catNewDB is a local
variable and will automatically be cleared when the routine ends.
hth,
Alan.

May 17 '06 #3
Thank you Cor

That is exactly what I needed. Someone to go through it like that and let
me know what is right and proper and what is not.

-Extremely Helpful.
I don't know how people programmed / got help and info before the internet.

I think I will try to use a normal db table and then once I learn my way
around vb, hit the sql side of things.
I don't know the proper terminology and still have yet to make a form with
some db stuff on it.
Don't want to kill my brain all at once - So I will try SQL Express on
version 2.0 :)

btw.
I return the lFileExists so that in my other function I might have to Code
around it if it was not
able to create the file ( for whatever reason ) and just load a splash
screen and say...try again later. Cant create db.
Or something

Thanks again,

Miro
"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:eJ******** ******@TK2MSFTN GP02.phx.gbl...
Miro,

We have a sample as this as well.
http://www.vb-tips.com/default.aspx?...1-d6dda3c888c8

Included is how to make tables in this sample. It uses only Com for the
part of the creation. If you use a Com, you are not really using Net you
use a DLL which has more chance on a problem than using solely Net. There
is in Net however no method to create an Access Database. If you are
learning I point you on the SQLExpress database. In my opinion is it
better to use that to study.

However I will give some comments on your program inline

Dim lFileExists As Boolean = False

' Without this "If statement" the Exception will catch if the file
already exists.
If Dir$("bla.abc") <> "" Then 'Returns the file name if it
exists.
lFileExists = True


Above is created the in Net included standard FileInfo class

Dim fi As New IO.FileInfo("c: \db1.mdb")
If fi.Exists Then

I advice you to use standard methods, which cannot give misunderstandin g
when you are doing maintenance or give the program to others.
Dim sCreateString As String
sCreateString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=bla.abc" ' & DatabaseFullPat h
catNewDB.Create (sCreateString)


A lot of work for a one time operation while it adds nothing (only
misunderstandin gs by those who read your program, what is beneath is at
least as good as)
catNewDB.Create ( "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=bla.abc" '
& DatabaseFullPat h)

I am not sure if the catNewDb.Create can throw an exception, if that is so
than it is better to use it, however do never this
Catch e As Exception


The "e" is standard used in all methods for events, therefore almost any
word is better than "e". By instance "ex".
'Later write a function to write this to a text error log.
MsgBox("An Exception Occurred - " + e.Message)

Try to avoid the + to concatenate Strings. For that is in VB the &. There
are situations where using the + can go wrong.
lFileExists = False

What is the sense of this above, I don't see it
catNewDB = Nothing

Setting something to nothing in VBNet at the end of a method has no sense
at all, than that it cost time. (This is classic VB use)

I hope this gives an idea.

Cor

May 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2763
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so that for example the following code would work as well in browsers as under Windows Scripting Host: str = "tableA";
0
3646
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
6
3694
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide the document to my clients so that they could use it to. I know most of them use Outlook XP or Outlook 2003, so what I created was a page that creates a Visual Basic script that, when saved to the desktop and
6
1476
by: TonyMast | last post by:
VB 2005 - XP Pro - Windows forms I'm trying to write a simple multiplication tables program for my little girl. How can I create 200 labels, 100 textboxes. Here is what I've tried so far (without luck). For looper As Int16 = 1 To 101 Dim lab As New Label
27
3799
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
4
3833
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without saying I'm not asking you to solve the question - that won't help come exam time :(). Anyway I have a CSV delimited file in the following format; STRING,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT ...
4
6917
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
3
3952
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
1
3388
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue; }; foo.cpp LFQueue<const char*,100Foo::lfqMyQueue;
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.