VB6 - Unbound DBGrid Data Entry
I see a lot of people trying to use the flexgrid controls (the old MSFlexGrid or the newer MSHFlexGrid controls) for data entry. They really were never intended for this. Even the VB6 manual suggests that you might want to do this in some circumstances, but the process involves fairly clunky floating of a manually created and managed Marquee Textbox over the grid.
Much of the time though, you can simply use the other grids provided with VB6 that were intended to be used for data entry: the DBGrid (DAO) or DataGrid (ADO, Unicode) controls. But many people either don't understand and fear data binding or simply can't use data binding. They need a grid that works in unbound modes like the flexgrids do.
Well if you need an unbound data entry grid and Unicode isn't a requirement, the older DBGrid works fine in unbound mode. Sadly the Unicode DataGrid doesn't support an unbound mode, probably because Microsoft found out that most programmers didn't really use DBGrids unbound anyhow.
In some cases the flexgrid controls may offer things you actually need, so you're back to floating a marquee Textbox around. But most of the time you don't need a flexgrid!
Why Don't People Use Unbound DBGrids?
I suspect there are several reasons for this:
- Most people program by copy/paste, and don't read the manuals. There is a dearth of examples on the Web for them to copy/paste from.
- The DBGrid documentation was left out of the VB6 manuals, and you have to go back to the VB5 or even VB4 maunals, which most people don't have or also won't read.
- This means the DBGrid documentation was also left out of the MSDN Library documentation, both on the Web and on the MSDN CDs for VB6. You have to use the old VB5-era .HLP file for this control, and most people will never think to look there.
- The programming model for Unbound DBGrids is like that of a custom DataSource control, and foreign to many programmers.
We can't convince people to read manuals (this is a lost cause). And if they don't even have them even the willing and able can't do so.
But we can plop another copy/paste demo out here on the Web!
This demo (attached) sets many of its DBGrid's properties at design time. However it sets up the column layout at runtime based on an INI file. This isn't required, and you can readily define your DBGrid columns in the IDE instead. For real programs you generally would do this in the IDE, but sometimes you want runtime flexibility too.
Beyond this column layout INI, the program loads/stores using a TXT file in VB's Write#/Input# (CSV-like) format with a little twist to handle storing quotes (") in String fields that normally can't have them. Users can enter quotes in the grid but load & store swap between quotes and vbFormFeed characters, a handy trick.
You should be able to just un-ZIP the attachment, open the VBP file, and Run.
The source code has comments to help you understand it, but for details you might want to crack open that DBGRID32.HLP file!
The Form in the demo has some labels with a few basic instructions on how to use special keystrokes in a data entry grid. I find that most people are a bit clueless about ESC & DEL keys and grid navigation (arrow keys, and TAB if you enable it in the grid).
I avoided Splits entirely here, because that seems to be an even more foreign topic to those who don't work with grid-based applications extensively.
When you want a data entry grid your best choice is a data-bound DataGrid control. If you insist on or must use an unbound data entry grid the DBGrid makes a good choice. However you do sacrifice Unicode capabilities - seldom a real problem anyway though since most other VB6 controls are not Unicode aware either.
So here is another example people can use to try to deal with grid-based data entry without creating a rat's nest of Marquee-management code and a bunch of buttons for things like "add row," "delete row," "cancel pending changes," and so on.
Digging deep, I did find this other example out there too: http://www.vb-helper.com/howto_dbgrid_unbound.html
Perhaps having both will help you figure things out!
Re: VB6 - Unbound DBGrid Data Entry
This was a very useful example. I liked the ini approach very much! Thank you very much for posting this!