I have an application that users enter in some data and query a data source. The recordsets returned are populated into a FlexGrid and later written to a file. At times there are recordsets that return NULL values for some of the fields. Whenever a recordset has a field with a NULL value I want the entire row to have a different background color. A single query may return several rows of records, but there may only be one that has any NULL values. Here is the code for the query and the population of the FlexGrid:
Private Sub cmdEnter_Click()
On Error GoTo errhandler
Dim strsql As String
Dim inti As Integer
Dim blnflag As Boolean
If txtItemCode.Text = "" Then
MsgBox "Item code cannot be empty, please enter Item Code", vbCritical
txtItemCode.SetFocus
Exit Sub
End If
strsql = " select gm_sku.sku_num, gm_sku.itm_cd, gm_itm.vsn, gm_sku.size_cd, gm_sku.color_des, gm_sku2upc_cd.upc_cd" & _
" from gm_sku, gm_itm , gm_sku2upc_cd" & _
" where gm_sku.sku_num = gm_sku2upc_cd.sku_num(+)" & _
" and gm_sku.itm_cd = gm_itm.itm_cd" & _
" and gm_itm.itm_cd = '" & txtItemCode.Text & "'"
Set rsmain = New ADODB.Recordset
rsmain.Open strsql, cn, adOpenKeyset, adLockReadOnly
If rsmain.EOF Then
MsgBox "UPC data data does not exist for this Item Code or its SKU's in the database...", vbCritical
txtItemCode.Text = ""
txtItemCode.SetFocus
Exit Sub
Else
blnflag = False
For inti = 1 To MSFItem.Rows - 1
If Left(MSFItem.TextMatrix(inti, 0), 9) = txtItemCode.Text Then
blnflag = True
Exit For
End If
Next
If blnflag = False Then
Do While Not rsmain.EOF()
If MSFItem.TextMatrix(1, 0) = "" Then
inti = 1
Else
inti = MSFItem.Rows
MSFItem.Rows = MSFItem.Rows + 1
End If
MSFItem.TextMatrix(inti, 0) = rsmain("sku_num")
MSFItem.TextMatrix(inti, 1) = rsmain("itm_cd")
MSFItem.TextMatrix(inti, 2) = rsmain("vsn")
MSFItem.TextMatrix(inti, 3) = IfNull(rsmain("size_cd"), "NULL")
MSFItem.TextMatrix(inti, 4) = IfNull(rsmain("color_des"), "NULL")
MSFItem.TextMatrix(inti, 5) = IfNull(rsmain("upc_cd"), "NULL")
rsmain.MoveNext
Loop
Else
MsgBox " This Item is already entered", vbInformation
End If
txtItemCode.Text = ""
txtItemCode.SetFocus
Exit Sub
End If
Exit Sub
errhandler:
MsgBox "Errors occured while retreiving the item information" & vbCrLf & "Please Click on Reset Button and Redo the process" & vbCrLf & Err.Number & ":" & Err.Description, vbCritical
Exit Sub
End Sub
...I use a simple Public Function to discern NULL values:
Function IfNull(value As Variant, Optional NullValue As Variant = "") As Variant
If IsNull(value) Then
IfNull = NullValue
Else
IfNull = value
End If
End Function
and here is the code to write the files:
Private Sub cmdSave_Click()
On Error GoTo errhandler
Dim blnWrite As Boolean
Dim strFile As String
Dim iFileNum As Integer
Dim inti As Integer
Dim strRec As String
Const updatecode = 2
If cboStoreNo.Text = "" Then
MsgBox "Please enter the Store number ", vbCritical
cboStoreNo.SetFocus
End If
If mskEffectiveDate.ClipText <> "" Then
If InvalidDate(mskEffectiveDate.Text, Me.Caption) Then
mskEffectiveDate.SetFocus
Exit Sub
End If
Else
MsgBox "Date cannot be empty, Please enter the date", vbCritical
mskEffectiveDate.SetFocus
Exit Sub
End If
If MSFItem.Rows > 2 Then
blnWrite = True
Else
If MSFItem.TextMatrix(1, 0) <> "" Then
blnWrite = True
Else
blnWrite = False
End If
End If
If blnWrite = True Then
OpenFile:
cdlFile.DefaultExt = "asc"
cdlFile.FileName = cboStoreNo.Text & "-" & Format(Date, "mmddyy") & "xreftxn.asc"
cdlFile.ShowSave
If cdlFile.FileName = "" Or cdlFile.FileName = "*.asc" Then
MsgBox "File name cannot be empty/ Invalid File name", vbCritical
GoTo OpenFile
End If
strFile = cdlFile.FileName
'Get a free file handle
iFileNum = FreeFile
Open strFile For Output As iFileNum
For inti = 1 To MSFItem.Rows - 1
strRec = updatecode & "," & """" & Replace(MSFItem.TextMatrix(inti, 0), "-", "") & """" & ",,,," & _
MSFItem.TextMatrix(inti, 5) & ","
Print #iFileNum, strRec
Next
Close iFileNum
MsgBox "File Generated", vbInformation
Call cmdReset_Click
Else
MsgBox "No Item Data has been entered to generate the File, Enter Item code and then click Save button", vbInformation
txtItemCode.SetFocus
Exit Sub
End If
Exit Sub
errhandler:
If Err.Number <> 32755 Then
MsgBox "Errors occured while writing to file" & vbCrLf & Err.Number & ":" & Err.Description, vbCritical
End If
Exit Sub
End Sub
...whenever a record contains NULL values and is colored differently I don't want that particular row to get written to the file, but the rest should. This has really got me perplexed?
Private Sub cmdEnter_Click()
On Error GoTo errhandler
Dim strsql As String
Dim inti As Integer
Dim blnflag As Boolean
If txtItemCode.Text = "" Then
MsgBox "Item code cannot be empty, please enter Item Code", vbCritical
txtItemCode.SetFocus
Exit Sub
End If
strsql = " select gm_sku.sku_num, gm_sku.itm_cd, gm_itm.vsn, gm_sku.size_cd, gm_sku.color_des, gm_sku2upc_cd.upc_cd" & _
" from gm_sku, gm_itm , gm_sku2upc_cd" & _
" where gm_sku.sku_num = gm_sku2upc_cd.sku_num(+)" & _
" and gm_sku.itm_cd = gm_itm.itm_cd" & _
" and gm_itm.itm_cd = '" & txtItemCode.Text & "'"
Set rsmain = New ADODB.Recordset
rsmain.Open strsql, cn, adOpenKeyset, adLockReadOnly
If rsmain.EOF Then
MsgBox "UPC data data does not exist for this Item Code or its SKU's in the database...", vbCritical
txtItemCode.Text = ""
txtItemCode.SetFocus
Exit Sub
Else
blnflag = False
For inti = 1 To MSFItem.Rows - 1
If Left(MSFItem.TextMatrix(inti, 0), 9) = txtItemCode.Text Then
blnflag = True
Exit For
End If
Next
If blnflag = False Then
Do While Not rsmain.EOF()
If MSFItem.TextMatrix(1, 0) = "" Then
inti = 1
Else
inti = MSFItem.Rows
MSFItem.Rows = MSFItem.Rows + 1
End If
MSFItem.TextMatrix(inti, 0) = rsmain("sku_num")
MSFItem.TextMatrix(inti, 1) = rsmain("itm_cd")
MSFItem.TextMatrix(inti, 2) = rsmain("vsn")
MSFItem.TextMatrix(inti, 3) = IfNull(rsmain("size_cd"), "NULL")
MSFItem.TextMatrix(inti, 4) = IfNull(rsmain("color_des"), "NULL")
MSFItem.TextMatrix(inti, 5) = IfNull(rsmain("upc_cd"), "NULL")
rsmain.MoveNext
Loop
Else
MsgBox " This Item is already entered", vbInformation
End If
txtItemCode.Text = ""
txtItemCode.SetFocus
Exit Sub
End If
Exit Sub
errhandler:
MsgBox "Errors occured while retreiving the item information" & vbCrLf & "Please Click on Reset Button and Redo the process" & vbCrLf & Err.Number & ":" & Err.Description, vbCritical
Exit Sub
End Sub
...I use a simple Public Function to discern NULL values:
Function IfNull(value As Variant, Optional NullValue As Variant = "") As Variant
If IsNull(value) Then
IfNull = NullValue
Else
IfNull = value
End If
End Function
and here is the code to write the files:
Private Sub cmdSave_Click()
On Error GoTo errhandler
Dim blnWrite As Boolean
Dim strFile As String
Dim iFileNum As Integer
Dim inti As Integer
Dim strRec As String
Const updatecode = 2
If cboStoreNo.Text = "" Then
MsgBox "Please enter the Store number ", vbCritical
cboStoreNo.SetFocus
End If
If mskEffectiveDate.ClipText <> "" Then
If InvalidDate(mskEffectiveDate.Text, Me.Caption) Then
mskEffectiveDate.SetFocus
Exit Sub
End If
Else
MsgBox "Date cannot be empty, Please enter the date", vbCritical
mskEffectiveDate.SetFocus
Exit Sub
End If
If MSFItem.Rows > 2 Then
blnWrite = True
Else
If MSFItem.TextMatrix(1, 0) <> "" Then
blnWrite = True
Else
blnWrite = False
End If
End If
If blnWrite = True Then
OpenFile:
cdlFile.DefaultExt = "asc"
cdlFile.FileName = cboStoreNo.Text & "-" & Format(Date, "mmddyy") & "xreftxn.asc"
cdlFile.ShowSave
If cdlFile.FileName = "" Or cdlFile.FileName = "*.asc" Then
MsgBox "File name cannot be empty/ Invalid File name", vbCritical
GoTo OpenFile
End If
strFile = cdlFile.FileName
'Get a free file handle
iFileNum = FreeFile
Open strFile For Output As iFileNum
For inti = 1 To MSFItem.Rows - 1
strRec = updatecode & "," & """" & Replace(MSFItem.TextMatrix(inti, 0), "-", "") & """" & ",,,," & _
MSFItem.TextMatrix(inti, 5) & ","
Print #iFileNum, strRec
Next
Close iFileNum
MsgBox "File Generated", vbInformation
Call cmdReset_Click
Else
MsgBox "No Item Data has been entered to generate the File, Enter Item code and then click Save button", vbInformation
txtItemCode.SetFocus
Exit Sub
End If
Exit Sub
errhandler:
If Err.Number <> 32755 Then
MsgBox "Errors occured while writing to file" & vbCrLf & Err.Number & ":" & Err.Description, vbCritical
End If
Exit Sub
End Sub
...whenever a record contains NULL values and is colored differently I don't want that particular row to get written to the file, but the rest should. This has really got me perplexed?