VISUAL BASIC PC Hardware | PC Software | Networking | Web Designs | News Letter | Home |
Visual Basic | C++ | Java |
DATABASE PROGRAMMING IN VB |
How do I use SQL in VB code?This sample code should give a very basic overview on how to access Jet databases using user-defined SQL strings in VB code. Note that for VB 4 and 5, with Jet 2+, Dynasets are considered evil. Use recordset. The Microsoft VB FAQ also contains some additional information. Why can't I use an index with my VB accessed database?There is a mistake in the 3.0 docs which says you
can set the active index for a recordset. You can't. The data control
uses the primary key for tables and physical order (I think) for
dynasets. NOTE: You can of course set the indexes yourself using code in VB/Pro (Table objects), but Data Controls can't. Sorry for the problems this possible misunderstanding caused! Thanks to John McGuire <jmcguire@jax.jaxnet.com> for clarifying this. "Can't find installable ISAM" or Why does my compiled VB database app generate an error when it ran just fine in the design environment?Applies to VB 3.0: You can thank Microsoft for documenting this topic so poorly. When you compile your VB database application, you must also have an INI file for it which provides the correct pointers to the appropriate database drivers. Therefore, if your application is named INVOICE.EXE, you will need to have a properly configured INVOICE.INI file in your Windows directory. The file, EXTERNAL.TXT, that came with VB should explain all about it. Is the Access Engine and Visual Basic Pro good enough for database work?After version 4.0, I will not hesitate to say: normally, yes! These are the major weaknesses of VBPro's database functions:
The good news is that lots of companies are willing to sell you products which address one or more of the above weaknesses. Also, if you build a database application with advanced database relations, it can be a good idea to build the database itself with Access and the front-end with VB. How do you avoid the "Invalid use of null" error when reading null values from a database?If you try to retrieve a null value (empty field) from a database, you will get the error: "Invalid use of Null". Here is one way to get around this problem: I've worked around this problem with the following code: TextBox.Text = MyTest.Fields("TestFld") & "" This code converts the Null-Value into an empty string. What is "NULL"?Contrary to popular belief, Null is not nothing. It's even less than nothing. 8^) The VB documentation describes all the horrors of misunderstanding the infamous NULL. Since people don't read the documentation, we feel like informing that If ThisVarIsNull = NULL then DoSomething will always fail, and the DoSomething can't possibly be executed. You must use IsNull(ThisVarIsNull) which will return True if the var is Null (phew!). If you want to find out why someone came to think of this strange value, some book on relational database theory should provide a few pointers. How can I access a record by record number?Use a counter or index field - or even better, a Bookmark property - for this. It is impossible to ask a relational database system to give you e.g. "field number 3 in record number 10" since by definition a relational database does not have row or column numbers. Databases allowing direct access like that is not even remotely relational. Access (and therefore, VB) is about as close to a real relational database system as you get. How about Access 2.0 compatibility?Applies to VB 3.0: You need the compatibility layer
availability. The file COMLYR.EXE is in the MSBASIC library on
Compuserve. This file provides all the items necessary for compatibility
between VB 3.0 and Access 2.0. For unknown reasons you can't install the compatibility layer without
Acess 2.0 being installed, even if you just want to open a database from
VB that was created by someone else. The file comlyr.exe can be downloaded from ftp.microsoft.com. It is located in the directory /softlib/mslfiles. (NOTE: A "DIR" in this directory is rarely a wise course of action. There is an enormous amount of files im /mslfiles.) Tips for VB database programming:
Note that some of the above may not apply to VB 4 and 5. Note that recordsets have now replaced dynasets and other methods. Calling stored queries may also be preferrable to direct SQL queries from your VB app. How come I get a "No Current Record" error when I use a a Data Control on an empty table?Well, this is a "feature" courtesy of Microsoft. KB article
Q106494
explains this in detail. Basically, the workaround is to add an empty
record to the table before the user can do anything (or before you try
to do any Moves on the Table). How can I speed up my VB database application?One word: stored queries. Failing that, recordsets. KB article Q109830 gives some hints. Things you should do include:
KB article Q120172 gives additional ideas on speeding up VB database access Major Weakness: SQL is SLOW! A hand-coded search (with indices) is MUCH faster than an equivalent SQL call, especially with complex search criteria. For example: SELECT * FROM Table WHERE SSN = '555-33-1234' AND Posted #01-31-95# is a lot slower than: Table.Index = "SSN" If Table("SSN")
"555-33-1234" Then Wend Granted, it is a LOT more code, but I ran a VERY similar query that
took THIRTY HOURS! The equivalent hand-written code took ELEVEN MINUTES!
That's 163 times faster! I think basically SQL isn't very good at
figuring out which indexes to use (I also think I've read something to
the effect that the newer version, 2.0 or 2.5, IS better at this). How do I get a bitmap picture in a field in an Access database?See p.466 of the Visual Basic (3.0) Programmer's Guide. It contains a section called "Using Bound Picture Box and Image Controls". Basically you have to bind the VB PictureBox to a field in the Access DB, set the .Picture property in the PictureBox, and then move to the next record or something. VB will then store your picture in Access in a form in which it can be retrieved by VB in the future. If you store the pictures in Access directly (using Access), VB won't be able to read them (using VB 3.0 and Access 1.1). You can also store the picture's filename as a text field in the
database and use LoadPicture() to load that file into the VB PictureBox. What is "Reserved Error -1209"?You will get a Reserved Error [-1209] ("There is no message for
this error") when your database is corrupted. Try opening the
database using MS Access; if it's corrupted you should get the option to
repair it. You should also compact it, after repair. I recommend you add the following to your File menu on your main form: Case ... "Cannot perform operation. illegal.." with Paradox 3.5 table(s)Your Paradox table must have a primary key, or it will be read-only
no matter what you set its properties to. I'm getting error message "Reserved Error [-nnnn] ("There is no message for this error")" from Jet Engine 2.0. Huh?See the Knowledge Base article Q117900 "Reserved Error Numbers Returned by the Jet 2.0 Engine" for a complete list of the new error messages. Extract: "Jet_Error/Message_String Why do I get "object not an array" when I try reference the fields of a global object variable which I have set to a table?VB has a parser bug which makes it difficult to use database objects declared in a module from within a form. WORKAROUND: Just perform some method on the table object somewhere before you try to reference fields. Say in a form-based subroutine AAAA_IllBeFirst you have a Tbl.MoveFirst, which is never even executed. Then VB suddenly realises what the object is and all is forgiven. Credit to Luke Webber and "Joe Foster of Borg". [Ayn Shipley <ashipley@hookup.net>] Steps for securing an Access 2.0 databaseWhat I started with: A database that I wanted to make secure. Access 2.0 installed on my hard drive. No Access permissions or accounts ever played with under Access.
See SetDefaultWorkspace and SetDataAccessOption in VB help for more information on how to set up the data access paramaters in an INI file. Make sure you add error handling for a missing or messed up SYSTEM.MDA file. I think this is a good basic way to prevent people from
looking/changing your database. Possible flaws are looking at a dump of
the .EXE file and finding the name and password used to open the
database (there are various workarounds for this) and also running a
program to look at parameters passed when opening the database (don't
know if this is possible and can't think of a workaround if it is).
|
For more details contact the webmaster |