home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic 4 Unleashed
/
Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso
/
source
/
chap36
/
gensql.bas
< prev
next >
Wrap
BASIC Source File
|
1995-07-27
|
3KB
|
63 lines
Attribute VB_Name = "basGenSQL"
'=====================================================================
'GENSQL.BAS by Frank Font 1995
'
'This VB4 file implements some general purpose SQL functions.
'=====================================================================
Option Explicit
'---------------------------------------------------------------------
'Returns sum of values in "SumField" of "MyDB" after applying the
'"From" and "Where" portions of the SQL statement.
'---------------------------------------------------------------------
Function SQLResultSum(MyDB As DATABASE, SumField As String, From$, Where$) As Long
Dim MyData As Recordset, sql$
sql$ = "Select sum(" + SumField + ") as [total] from " + From$ + _
IIf(Len(Trim$(Where$)) > 0, " where " + Where$, "")
Set MyData = MyDB.OpenRecordset(sql$, dbOpenDynaset)
If MyData.RecordCount = 0 Then
SQLResultSum = 0 'Send empty string.
Else
MyData.MoveLast
SQLResultSum = MyData![total]
End If
End Function
'---------------------------------------------------------------------
'Returns value of field named in "aField$" of "MyDB" after applying the
'"From" and "Where" portions of the SQL statement.
'This only returns the value in last record. If the record is not
'found, the empty string is returned.
'---------------------------------------------------------------------
Function SQLResultStr(MyDB As DATABASE, aField$, From$, Where$) As String
Dim MyData As Recordset, sql$
sql$ = "Select " + aField + " as [result] from " + From$ + _
IIf(Len(Trim$(Where$)) > 0, " where " + Where$, "")
Set MyData = MyDB.OpenRecordset(sql$, dbOpenDynaset)
If MyData.RecordCount = 0 Then
SQLResultStr = "" 'Send empty string.
Else
MyData.MoveLast
SQLResultStr = MyData![result]
End If
End Function
'---------------------------------------------------------------------
'Returns number of records in set defined by the "From" and "Where".
'---------------------------------------------------------------------
Function SQLResultCount(MyDB As DATABASE, From$, Where$) As Long
Dim MyData As Recordset, sql$
sql$ = "Select count(*) as [total] from " + From$ + _
IIf(Len(Trim$(Where$)) > 0, " where " + Where$, "")
Set MyData = MyDB.OpenRecordset(sql$, dbOpenDynaset)
If MyData.RecordCount = 0 Then
SQLResultCount = 0
Else
MyData.MoveLast
SQLResultCount = MyData![total]
End If
End Function