VB 6.0 - Problem of True DBGrid (Apex) - Asked By Dhrubajyoti Dey on 18-Jul-14 12:55 PM

I am using TrueDBGrid of Apex. My grid is bound with a recordset object, now for some columns I don't want null values. I have trapped AfterColUpdate event succcessfully (?) but what if user just press enter/tab with entering any value in the field? In this case AfterColUpdate or BeforeColUpdate doesn't fire.
So what I did, maintained a dirty flag to check whether user entering in a new row, if yes don't allow navigation by trapping BeforeColUpdate event. After insert/delete/update operation I allowed the user to navigate the empty columns of last row. It's working, but can you suggest me a better idea, or it's the best solution I achieved!
My code is given below, pls have a close look on all events.
'-------------- Code...
Option Explicit
Private dsObject As CData
Private rsObject As ADODB.Recordset
Private colNonEmpty As Collection
Private blnDirty As Boolean
Private Function Exists(ByVal Code As String) As Boolean
Dim rsSearch As ADODB.Recordset
Set rsSearch = rsObject.Clone(adLockReadOnly)
With rsSearch
If .RecordCount > 0 Then
.Find "Code='" & Code & "'"
If Not .EOF Then Exists = True
End If
End With
Set rsSearch = Nothing
End Function
Private Sub cmdSave_Click()
On Error GoTo ErrHandler
dsObject.SaveData
ExitBlock:
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "Error"
GoTo ExitBlock
End Sub
Private Sub Form_Initialize()
Set dsObject = New CData
End Sub
Private Sub Form_Load()
Set rsObject = dsObject.Recordset
Set colNonEmpty = New Collection
With tdgReg
Set .DataSource = rsObject
.ColumnFooters = True
.Columns(2).NumberFormat = "#########0.00"
.Columns(3).NumberFormat = "0.00"
.Columns(3).Locked = True
.Columns(3).AllowFocus = False
End With
'These are the columns I don't want NULL
colNonEmpty.Add 0, "C0"
colNonEmpty.Add 1, "C1"
colNonEmpty.Add 2, "C2"
End Sub
Private Sub Form_Terminate()
Set dsObject = Nothing
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set tdgReg.DataSource = Nothing
Set colNonEmpty = Nothing
End Sub
Private Sub tdgReg_AfterDelete()
ShowSummary
blnDirty = False
End Sub
Private Sub tdgReg_AfterInsert()
ShowSummary
blnDirty = False
End Sub
Private Sub tdgReg_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
Select Case ColIndex
Case 0
If tdgReg.Text = "" Then
MsgBox "No information entered. Data required.", vbExclamation, "Data Error"
Cancel = True
ElseIf Exists(tdgReg.Text) Then
MsgBox "Code already exists. Duplicacy not allowed here.", vbExclamation, "Duplicacy Error"
Cancel = True
End If
Case 1
If tdgReg.Text = "" Then
MsgBox "No information entered. Data required.", vbExclamation, "Data Error"
Cancel = True
End If
Case 2
If tdgReg.Text = "" Or Val(tdgReg.Text) <= 0 Then
MsgBox "Basic pay should hold some positive value.", vbExclamation, "Data Error"
Cancel = True
End If
End Select
End Sub
Private Sub tdgReg_BeforeInsert(Cancel As Integer)
blnDirty = True
End Sub
Private Sub tdgReg_BeforeRowColChange(Cancel As Integer)
If Not blnDirty Then Exit Sub
If tdgReg.Text = "" And Not (AllowEmptyColumn(tdgReg.Col)) Then
Cancel = True
MsgBox "No information entered. Data required.", vbExclamation, "Data Error"
End If
End Sub
Private Function AllowEmptyColumn(ByVal ColNo As Integer) As Boolean
Dim intRetValue As Integer
On Error GoTo ErrHandler
'Just try to access the member via key
'If it's not present in the collection, it will raise error
intRetValue = colNonEmpty("C" & ColNo)
AllowEmptyColumn = False
Exit Function
ErrHandler:
AllowEmptyColumn

Hmmm... - Asked By Robbe Morris on 18-Jul-14 12:55 PM

I use the VS Flex Grid here at Gartner so I'm not totally familiar with the APEX grid.  Your code appears fine to me.
One thing sort of off subject though that might help down the road in controlling events.
I established a global property (or variable) that checks to see if I want to allow code in the event to be processed.
The first line of each event, I put:
if gPreventFormEvents = True then Exit Sub
If I ever opt to run a function that I specifically want to exclude "other" grid functions from being triggered, I just set this variable to True and release it back to false at the end of the function.  I've also found that this can speed up the grid loads.
These grids often fire events in places we don't want them to.  I found this little diddy to be quite helpful.