Paradox
method Check_Data()
var
testTableTC TCursor
DateOfBirth, AgeOnThisDate Date
CalculatedAge SmallInt
Error_Count SmallInt
endvar
testTableTC.open("testdate")
testTableTC.edit()
Error_Count = 0
scan testTableTC : ; The colon is required!
DateOfBirth = testTableTC."Date of Birth"
AgeOnThisDate = testTableTC."Age on This Date"
; Check for a valid comparison
if DateOfBirth > AgeOnThisDate then
testTableTC."Computed Age" = blank()
loop ; Get next record from table
endif
CalculatedAge = subject.Determine_Age(DateOfBirth, AgeOnThisDate)
testTableTC."Computed Age" = CalculatedAge
if testTableTC."Correct Age" <> CalculatedAge then
Error_Count = Error_Count + 1
endif
endscan
testTableTC.close()
TESTDATE_UI_TableFrame.forceRefresh()
Number_of_Errors_UI_Field = Error_Count
if Number_of_Errors_UI_Field = 0
then Number_of_Errors_UI_Field.Font.Color = Green
else Number_of_Errors_UI_Field.Font.Color = Red
endif
endmethod Check_Data
MS Access 97
Individual fields are referenced via
Recordset_X!FieldName Recordset_x![FieldName]Use CurrentDB to refer to the current database. In the following, the word Set is required. It causes a variable to point to (reference) the object.
Dim dbsA As Database Set dbsA = CurrentDB
Dim dbs As Database
Dim Some_Recordset As Recordset
Dim SQL_Str As String
' Return reference to current database.
Set dbs = CurrentDb
SQL_Str = "SELECT * FROM Orders WHERE [StateField] = 'VA'"
Set Some_Recordset = dbs.OpenRecordset(SQL_Str)
With Some_Recordset
Do While Not .EOF
TempStr = !Field1 & "some text" & (10 * !Field2) & vbCr
Debug.Print TempStr
.MoveNext
Loop
End With
Some_Recordset.Close
Set dbs = Nothing
More Details
Example
Private Sub Button20_Click()
Dim MyDB As Database, MyTable As Recordset
Dim iBDOY As Integer, iDOY As Integer, iAge As Integer
Set MyDB = DBEngine.Workspaces(0).Databases(0) ' Older syntax
Set MyTable = MyDB.OpenRecordset("testdate", DB_OPEN_TABLE) ' Open table
MyTable.MoveFirst ' Locate first record
Do Until MyTable.EOF ' Begin loop
MyTable.Edit ' Enable editing
iDOY = DateDiff("y", "1-Jan", Format$(MyTable.[Age on This Date], "dd-mmm")) + 1
iBDOY = DateDiff("d", "1-Jan", Format$(MyTable.[Date of Birth], "dd-mmm")) + 1
iAge = Year(MyTable.[Age on This Date]) - Year(MyTable.[Date of Birth])
If iBDOY > iDOY Then iAge = iAge - 1
MyTable.[Computed Age] = iAge
MyTable.Update ' Save changes
MyTable.MoveNext ' Locate next record
Loop ' End of loop
MyTable.Close ' Close table
[Computed Age].Requery
End Sub
Note that instead of using
Set MyDB = DBEngine.Workspaces(0).Databases(0) ' Older syntax
Set MyTable = MyDB.OpenRecordset("testdate", DB_OPEN_TABLE) ' Open table
with Access 97, you could use either
Set MyTable = CurrentDB.OpenRecordset("testdate", DB_OPEN_TABLE) ' Open table
Set MyTable = CurrentDB.OpenRecordset("testdate", dbOpenTable) ' Open table
Here is an alternate method to walk through a table. However, there is no good way to determine when the last record has been processed. (BTW, this algorithm gives incorrect answers.)
DoCmd.GoToRecord , , A_FIRST ' Locate first record.
Do Until 1 = 0 ' Begin infinite loop
If [Computed Age] = 0 Then Exit Do ' Get out of the loop.
[Computed Age] = _
Int(([Age on This Date] - [Date of Birth]) / 365.25 + 0.5) - 1
DoCmd.GoToRecord ' Locate next record.
Loop ' End of loop.
MS Access XP
In the help, all the old VisualBasic (ie, MS Access 97) commands are are still listed ... but don't try to use them - they don't work! For instance
Dim dbsExample As Databaseproduces
Compile Error: User-defined type not definedInstead of the commands listed in the help, you have to use something like this.
Dim conDatabase As ADODB.Connection
Dim Some_Recordset As ADODB.Recordset
Dim SQL_Str As String
' Return reference to current database.
SQL_Str = "SELECT * FROM Batch_Preparation_tbl " + _
"WHERE [Batch_ID] = " + Batch_Name_UIEdit.Value + _
" and [SOP_Step] = '1'"
Set conDatabase = CurrentProject.Connection
Set Some_Recordset = New Recordset
Some_Recordset.Open SQL_Str, conDatabase
With Some_Recordset
Performed_By_UIEdit.Value = !Performed_By
Date_Prepared_UIEdit.Value = ![Date/Time]
' Debug.Print TempStr
End With
Some_Recordset.Close
conDatabase.Close
Set Some_Recordset = Nothing
Set conDatabase = Nothing
A Fix
Tools / References...
Microsoft DAO 3.6 Object Library(The version is probably machine dependent.) This fixes MOST of the problems using the old examples ... but some variable declarations still cause a problem. In the following example, I've show 3 different ways to declare MyTable - in a real example, it should only be declared once.
Dim dbsExample As Database ' This works in both versions
Dim MyTable As Recordset ' This still fails in XP, but works with MS Access 97
Dim MyTable ' Declaring a variant works in XP
Dim MyTable As DAO.Recordset ' This works in XP
Set MyTable = CurrentDb.OpenRecordset("Test_LU", dbOpenTable)
Of course, I have never found any documentation about this ...
instead, one of the people who read these pages gave me the tip.
Delphi 5.0
Blob Fields
| Class | Source File | Comment | |||
|---|---|---|---|---|---|
| TMemoryStream | classes.pas | Converts a buffer (block of memory) to/from a stream | |||
| TBlobStream | dbtables.pas | Used with BLOB fields | |||
| TIBBlobStream | ibblob.pas | Interbase BLOB fields - more properties and methods than TBlobStream | |||
| TIBDSBlobStream | ibcustomdataset.pas | Interbase Data Set BLOB fields - for internal use | |||
Examples in the Delphi help
| Example | Description |
|---|---|
| TBlobStream.Create
TIBBlobStream.Create | How to copy a blob field from one table to another |
| TBlobStream.Read | How to read a blob into a PChar Buffer |
| TBlobStream.Write | How to add data to the end of a blob |
This reads data using a Memory stream
procedure TForm1.PlotSpectra();
var
i:integer ;
ar: array[0..315] of single;
xx: TMemoryStream ;
begin
xx := TMemoryStream.Create;
try
(Table1.FieldByName('SPECTRUM_Y') as TBlobField).SaveToStream(xx) ; // read a blob
// xx.Seek(0,soFromBeginning); // eqivalent to xx.position := 0
xx.Position := 0;
xx.Read(ar, 316*4); // 4 bytes per element, copies the data to the array
Image1.Canvas.FillRect(Rect(0,0,image1.Width, image1.Height));
Image1.Canvas.MoveTo(0, 0);
for i := 0 to 315 do begin
Image1.Canvas.LineTo(i, trunc(ar[i] * image1.Height/100));
end;
finally
xx.Free ;
end; // try
end;
ASP
This example uses a connection object to access an already defined ODBC connection.
If you are opening several recordsets on the same database, it is better to use a connection object than to define the parameters in the recordset.open command - it is better and faster to create one connection instead of 5.
This is an example of a processing loop. The 2 nested loops produce an interesting ouput - most of the code is omitted in this example, there is just enough to give you the idea. The html table is defined outside the loop, only the rows are generated inside the loop.
References:
Restarting IIS