HOWTO: Copy Current Database Record into a Record VariableID: Q97413
|
Although Visual Basic version 3.0 for Windows does not provide a
direct way to assign the current database record to a record variable,
this article gives you a generic routine. Using this generic routine,
you can assign the current record, containing any number of fields,
to a record variable that represents the structure of the current
database record.
This generic routine is useful if you have existing database code that
uses record variables to represent database records. For example, using
this routine, you can use the Visual Basic data access features without
making major changes to how you read and handle records. After you
assign the contents of the current record to a record variable of the
appropriate type, your code can manipulate the record as before,
independent of the underlying database.
The routine demonstrated below requires Windows version 3.1 or later
because it uses the Windows API function hmemcpy(), which was
introduced in Windows version 3.1. An error will result on the call to
hmemcpy() if you attempt to run the sample using Windows version 3.0.
Follow these general steps to assign the current database record to a record variable:
Control Name Property New Value Comment
------------------------------------------------------------------
Command1 Caption "Copy"
Data1 DatabaseName BIBLIO.MDB You will also need to
provide the full path to
this file, which should
be in your Visual Basic
directory C:\VB
Data1 RecordSource Authors
Text1 DataSource Data1
Text1 DataField AU_ID
Text2 DataSource Data1
Text2 DataField Author
Type typeAuthor
AU_ID As Long
Author As String * 255
End Type
' Enter the following Declare on a single line:
Declare Sub hmemcpy Lib "KERNEL" (dest As Any, src As Any, ByVal
Size As Long)
Function GetCurrRec (ds As Dynaset) As String
Dim i As Integer
Static FieldStr As String
Static recStr As String
recStr = ""
'Step through each field in the current record and accumulate
'the contents of each field into a string
For i = 0 To ds.Fields.Count - 1
'Pad out to the right size
FieldStr = Space(ds.Fields(i).Size)
Select Case ds.Fields(i).Type
'Copy the binary representation of the field to a
'string (FieldStr)
Case 1, 2 'Bytes
hmemcpy ByVal FieldStr, CInt(ds.Fields(i).Value),
ds.Fields(i).Size
Case 3 'Integers
hmemcpy ByVal FieldStr, CInt(ds.Fields(i).Value),
ds.Fields(i).Size
Case 4 'Long integers
hmemcpy ByVal FieldStr, CLng(ds.Fields(i).Value),
ds.Fields(i).Size
Case 5 'Currency
hmemcpy ByVal FieldStr, CCur(ds.Fields(i).Value),
ds.Fields(i).Size
Case 6 'Singles
hmemcpy ByVal FieldStr, CSng(ds.Fields(i).Value),
ds.Fields(i).Size
Case 7, 8 'Doubles
hmemcpy ByVal FieldStr, CDbl(ds.Fields(i).Value),
ds.Fields(i).Size
Case 9, 10 'String types
hmemcpy ByVal FieldStr, ByVal CStr(ds.Fields(i).Value),
Len(ds.Fields(i).Value)
Case 11, 12 'Memo and long binary
FieldStr = ds.Fields(i).GetChunk(0, ds.Fields(i).FieldSize())
End Select
'Accumulate the field string into a record string
recStr = recStr & FieldStr
Next
'Return the accumulated string containing the contents of all
'fields in the current record
GetCurrRec = recStr
End Function
Sub Command1_Click ()
Dim recAuthor As typeAuthor
Dim strCurrRec As String
Dim strVerify As String
'Copy the current record in the Authors table to a string
strCurrRec = GetCurrRec(Data1.RecordSet)
'Copy the string to the record variable that has a structure
'matching the struture of the current record in the Authors table
hmemcpy recAuthor, ByVal strCurrRec, Len(recAuthor)
'Verify that the correct results were returned by displaying
'the contents of the record variable
strVerify = "AU_ID: " & Format$(recAuthor.AU_ID) & Chr$(13)
strVerify = strVerify & "Author: " & Trim(recAuthor.Author)
MsgBox strVerify
End Sub
Additional query words:
Keywords : kbVBp300
Version : WINDOWS:3.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 12, 1999