home *** CD-ROM | disk | FTP | other *** search
- 'copyright (c) 1994 by Bruce Fulton
- 'All Rights Reserved
- 'You may use this program for your own education
- 'and information, and you may give a copy of the program,
- 'completely intact, to others to help them learn, but
- 'you may not charge for the program nor may you charge
- 'any fee for copying it for others.
- Option Explicit
-
- 'program declarations
- Global ThePath As String
- Global Const MB_YESNO = 4 ' Yes and No buttons
- Global Const IDYES = 6 ' Yes button pressed
- Global Const MB_DEFBUTTON2 = 256 ' Second button is default
-
-
- ' from the data.txt constant file
- ' Data Access constants
- ' not all of these are used in this program
- '
- ' Option argument values (CreateDynaset, etc)
- Global Const DB_DENYWRITE = &H1
- Global Const DB_DENYREAD = &H2
- Global Const DB_READONLY = &H4
- Global Const DB_APPENDONLY = &H8
- Global Const DB_INCONSISTENT = &H10
- Global Const DB_CONSISTENT = &H20
- Global Const DB_SQLPASSTHROUGH = &H40
-
- ' SetDataAccessOption
- Global Const DB_OPTIONINIPATH = 1
-
- ' Field Attributes
- Global Const DB_FIXEDFIELD = &H1
- Global Const DB_VARIABLEFIELD = &H2
- Global Const DB_AUTOINCRFIELD = &H10
- Global Const DB_UPDATABLEFIELD = &H20
-
- ' Field Data Types
- Global Const DB_BOOLEAN = 1
- Global Const DB_BYTE = 2
- Global Const DB_INTEGER = 3
- Global Const DB_LONG = 4
- Global Const DB_CURRENCY = 5
- Global Const DB_SINGLE = 6
- Global Const DB_DOUBLE = 7
- Global Const DB_DATE = 8
- Global Const DB_TEXT = 10
- Global Const DB_LONGBINARY = 11
- Global Const DB_MEMO = 12
-
- ' TableDef Attributes
- Global Const DB_ATTACHEXCLUSIVE = &H10000
- Global Const DB_ATTACHSAVEPWD = &H20000
- Global Const DB_SYSTEMOBJECT = &H80000002
- Global Const DB_ATTACHEDTABLE = &H40000000
- Global Const DB_ATTACHEDODBC = &H20000000
-
- ' ListTables TableType
- Global Const DB_TABLE = 1
- Global Const DB_QUERYDEF = 5
-
- ' ListTables Attributes (for QueryDefs)
- Global Const DB_QACTION = &HF0
- Global Const DB_QCROSSTAB = &H10
- Global Const DB_QDELETE = &H20
- Global Const DB_QUPDATE = &H30
- Global Const DB_QAPPEND = &H40
- Global Const DB_QMAKETABLE = &H50
-
- ' ListIndexes IndexAttributes values
- Global Const DB_UNIQUE = 1
- Global Const DB_PRIMARY = 2
- Global Const DB_PROHIBITNULL = 4
- Global Const DB_IGNORENULL = 8
- ' ListIndexes FieldAttributes value
- Global Const DB_DESCENDING = 1 'For each field in Index
-
- ' CreateDatabase and CompactDatabase Language constants
- Global Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"
- Global Const DB_LANG_SPANISH = ";LANGID=0x040A;CP=1252;COUNTRY=0"
- Global Const DB_LANG_DUTCH = ";LANGID=0x0413;CP=1252;COUNTRY=0"
- Global Const DB_LANG_SWEDFIN = ";LANGID=0x040C;CP=1252;COUNTRY=0" 'VB3 andAccess 1.1 Databases
- Global Const DB_LANG_NORWDAN = ";LANGID=0x0414;CP=1252;COUNTRY=0" 'VB3 andAccess 1.1 Databases
- Global Const DB_LANG_ICELANDIC = ";LANGID=0x040F;CP=1252;COUNTRY=0" 'VB3 andAccess 1.1 Databases
- Global Const DB_LANG_NORDIC = ";LANGID=0x041D;CP=1252;COUNTRY=0" 'Access 1.0 Databases only
-
- ' CreateDatabase and CompactDatabase options
- Global Const DB_VERSION10 = 1 ' Microsoft Access Version 1.0
- Global Const DB_ENCRYPT = 2 ' Make database encrypted.
- Global Const DB_DECRYPT = 4 ' Decrypt database while compacting.
-
- 'Collating order values
- Global Const DB_SORTGENERAL = 256 ' Sort by EFGPI rules (English, French, German,Portuguese, Italian)
- Global Const DB_SORTSPANISH = 258 ' Sort by Spanish rules
- Global Const DB_SORTDUTCH = 259 ' Sort by Dutch rules
- Global Const DB_SORTSWEDFIN = 260 ' Sort by Swedish, Finnish rules
- Global Const DB_SORTNORWDAN = 261 ' Sort by Norwegian, Danish rules
- Global Const DB_SORTICELANDIC = 262 ' Sort by Icelandic rules
- Global Const DB_SORTPDXINTL = 4096 ' Sort by Paradox international rules
- Global Const DB_SORTPDXSWE = 4097 ' Sort by Paradox Swedish, Finnish rules
- Global Const DB_SORTPDXNOR = 4098 ' Sort by Paradox Norwegian, Danish rules
- Global Const DB_SORTUNDEFINED = -1 ' Sort rules are undefined or unknown
-
- Sub addfield ()
- 'turn on the errorhandler
- On Error GoTo addfieldERR
- screen.MousePointer = 11
-
- 'dim variables as a database and field objects
- Dim db As database
- Dim newf As New field
-
- 'define the name, the type and if applicable,
- 'the length and attributes for the field.
- newf.Name = "Comment"
- newf.Type = DB_MEMO
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'add the new field to the Place Names table in the database
- db.TableDefs("Place Names").Fields.Append newf
- 'close the database
- db.Close
- screen.MousePointer = 0
- MsgBox "Field 'Comments' successfully added to Place Names."
-
-
- 'error trapping routine
- GoTo addfieldEND
- addfieldERR:
- showerror
- Resume addfieldEND
- addfieldEND:
- screen.MousePointer = 0
-
- End Sub
-
- Sub additems (lbl As Label)
- 'turn on error trapping
- On Error GoTo additemsERR
- screen.MousePointer = 11
-
- 'declare needed variables
- Dim filnam, lin As String
- Dim db As database, tb As table
- Dim elapsed, itmcnt As Long
-
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB", True)
- 'select/open the table to add to
- Set tb = db.OpenTable("Place Names")
-
- 'we'll read data in from a
- 'fixed field ascii file and add it to the mdb database.
- 'You could also load from other file formats or from
- 'values in text boxes.
- filnam = ThePath + "sample.dta"
- Open filnam For Input As #1
- 'just skip any duplicate key errors
- On Error Resume Next
- 'let's see how long it takes
- elapsed = Timer
- While Not EOF(1)
- 'experiment with adding or commenting out the
- 'following two statements to see the performance
- 'hit!
-
- 'DoEvents
- 'FreeLocks
-
- 'use the addnew method
- tb.AddNew
- Line Input #1, lin
- tb("Name") = Trim$(Mid$(lin, 1, 48))
- lbl.Caption = "Adding " & tb("Name")
- tb("State Code") = Val(Mid$(lin, 60, 2))
- tb("County Code") = Val(Mid$(lin, 62, 3))
- 'str2dec converts latitude/longitude
- 'in dddhhmm format to decimal format
- tb("Latitude") = str2dec(Mid$(lin, 73, 6))
- tb("Longitude") = str2dec(Mid$(lin, 80, 7))
- 'if you don't 'update', the data is not added
- tb.Update
- If Err <> 0 Then
- lbl.Caption = "ERROR - did not add " & tb("Name")
- Err = 0
- Else
- 'just counting how many items we've done
- itmcnt = itmcnt + 1
- End If
- 'save some time by commenting out the label refresh
- 'command
- lbl.Refresh
- Wend
-
- 'restore regular error handler and close everything
- On Error GoTo additemsERR
- tb.Close
- db.Close
- Close #1
- form1.Label3.Caption = ""
- screen.MousePointer = 0
- 'how did we do?
- elapsed = Timer - elapsed
- MsgBox Str$(itmcnt) & " items successfully added in " & Str$(elapsed) & " seconds."
-
- 'error trapping routine
- GoTo additemsEND
- additemsERR:
- showerror
- Resume additemsEND
- additemsEND:
- screen.MousePointer = 0
- End Sub
-
- Sub addnameidx ()
- 'turn on the errorhandler
- On Error GoTo addnameidxERR
- screen.MousePointer = 11
-
- 'dim database, new index objects
- Dim db As database
- Dim ix As New Index
- Dim elapsed
- 'name the new table
- ix.Name = "Name Index"
- ix.Fields = "Name"
- ix.Unique = False
- ix.Primary = False
- 'let's see how long it took
- elapsed = Timer
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'add the new table to the database
- db.TableDefs("Place Names").Indexes.Append ix
- 'close the database
- db.Close
- elapsed = Timer - elapsed
- screen.MousePointer = 0
- MsgBox "Secondary index on Name for table Place Names successfully created. It took " & Str$(elapsed) & " seconds."
-
- 'error trapping routine
- GoTo addnameidxEND
- addnameidxERR:
- showerror
- Resume addnameidxEND
- addnameidxEND:
- screen.MousePointer = 0
-
- End Sub
-
- Sub addtucson ()
- 'turn on error trapping
- On Error GoTo addtucsonERR
- screen.MousePointer = 11
-
- 'declare needed variables
- Dim db As database, tb As table
-
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'select the table to add to
- Set tb = db.OpenTable("Place Names")
-
- 'start addnew method
- tb.AddNew
- tb("Name") = "Tucson"
- tb("State Code") = 4
- tb("County Code") = 19
- tb("Latitude") = 32.2667
- tb("Longitude") = 111.0083
- 'perform the update
- tb.Update
- 'If Tucson is already in the database, this will
- 'generate a dupe primary key error and not add the item.
- 'If the primary key index has not been defined, then
- 'you can add this item, but when you try to add the
- 'primary index, you will get an error.
- 'close the table and database
- tb.Close
- db.Close
- screen.MousePointer = 0
- MsgBox "Tucson successfully added"
-
- On Error GoTo addtucsonERR
-
- 'error trapping routine
- GoTo addtucsonEND
- addtucsonERR:
- showerror
- Resume addtucsonEND
- addtucsonEND:
- screen.MousePointer = 0
- End Sub
-
- Sub killmdb ()
- On Error GoTo killmdbERR
- Dim filspec, s
- filspec = ThePath & "USPLACE.MDB"
- Kill filspec
- filspec = ThePath & "USPLACE.LDB"
- Kill filspec
- s = ThePath & "USPLACE.MDB successfully deleted."
- MsgBox s
-
- 'error trapping routine
- GoTo killmdbEND
- killmdbERR:
- showerror
- Resume killmdbEND
- killmdbEND:
-
- End Sub
-
- Sub makedb ()
- 'turn on error handler
- On Error GoTo makedbERR
- screen.MousePointer = 11
-
- 'dim a variable as a database object
- Dim Newdb As database
-
- 'create the database
- Set Newdb = CreateDatabase(ThePath + "USPLACE.MDB", DB_LANG_GENERAL)
- 'close the database
- Newdb.Close
-
- screen.MousePointer = 0
- MsgBox "Database USPLACE.MDB successfully created."
-
- 'error trapping routine
- GoTo makedbEND
- makedbERR:
- showerror
- Resume makedbEND
- makedbEND:
- screen.MousePointer = 0
- End Sub
-
- Sub makeindex ()
- 'turn on the errorhandler
- On Error GoTo makeindexERR
- screen.MousePointer = 11
-
- 'dim database, new index objects
- Dim db As database
- Dim ix As New Index
-
- 'name the new index and set properties
- ix.Name = "Lat_Long Index"
- ix.Fields = "Latitude;Longitude"
- ix.Unique = True
- ix.Primary = True
-
-
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'add the new index to the table in the database
- db.TableDefs("Place Names").Indexes.Append ix
- 'close the database
- db.Close
- screen.MousePointer = 0
- MsgBox "Primary Key for table Place Names successfully created."
-
- 'error trapping routine
- GoTo makeindexEND
- makeindexERR:
- showerror
- Resume makeindexEND
- makeindexEND:
- screen.MousePointer = 0
-
- End Sub
-
- Sub maketable ()
- 'turn on the errorhandler
- On Error GoTo maketableERR
- screen.MousePointer = 11
-
- 'dim variables as a database, table, field and index objects
- Dim db As database
- Dim Newtd As New tabledef
- ReDim newf(6) As New field
-
-
- 'name the new table
- Newtd.Name = "Place Names"
-
- 'you must append at least one field when you create
- 'a table. you can, of course, do more than one, but
- 'you must do at least one
-
- 'for each field, set the name, the type and if applicable,
- 'the length and attributes. Then, append the new fields
- 'to the table object
- newf(1).Name = "RecID"
- newf(1).Type = DB_LONG
- newf(1).Attributes = DB_AUTOINCRFIELD
- Newtd.Fields.Append newf(1)
-
- newf(2).Name = "Name"
- newf(2).Type = DB_TEXT
- newf(2).Size = 48
- Newtd.Fields.Append newf(2)
-
- newf(3).Name = "State Code"
- newf(3).Type = DB_INTEGER
- Newtd.Fields.Append newf(3)
-
- newf(4).Name = "County Code"
- newf(4).Type = DB_INTEGER
- Newtd.Fields.Append newf(4)
-
- newf(5).Name = "Latitude"
- newf(5).Type = DB_CURRENCY
- Newtd.Fields.Append newf(5)
-
- newf(6).Name = "Longitude"
- newf(6).Type = DB_CURRENCY
- Newtd.Fields.Append newf(6)
-
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'add the new table to the database
- db.TableDefs.Append Newtd
- 'close the database
- db.Close
- screen.MousePointer = 0
- MsgBox "Table Place Names successfully created."
-
- 'error trapping routine
- GoTo maketableEND
- maketableERR:
- showerror
- Resume maketableEND
- maketableEND:
- screen.MousePointer = 0
- End Sub
-
- Sub seekname ()
- 'turn on the errorhandler
- On Error GoTo seeknameERR
- 'dim database, new index objects
- Dim db As database
- Dim tb As table
- Dim ix As Index
- Dim tmpstr, crlf As String
- crlf = Chr$(13) + Chr$(10)
- 'open the database
- Set db = OpenDatabase(ThePath + "USPLACE.MDB")
- 'open the table
- Set tb = db.OpenTable("Place Names")
- 'specify the index
- tb.Index = "Name Index"
- 'perform the seek:
- tb.Seek ">=", InputBox$("Name to look for:")
- If tb.NoMatch Then
- MsgBox "Record not found."
- Else
- tmpstr = tb("name") & crlf & "Latitude: " & Str$(tb("Latitude")) & crlf & "Longitude: " & Str$(tb("Longitude"))
- MsgBox tmpstr
- End If
- tb.Close
- db.Close
-
- 'error trapping routine
- GoTo seeknameEND
- seeknameERR:
- showerror
- Resume seeknameEND
- seeknameEND:
- screen.MousePointer = 0
-
-
- End Sub
-
- Sub showerror ()
- Dim s As String
- Dim crlf As String
-
- crlf = Chr(13) + Chr(10)
- s = "The following Error occurred:" + crlf + crlf
- 'add the error string
- s = s + Error$ + crlf
- 'add the error number
- s = s + "Number: " + CStr(Err)
- 'beep and show the error
- Beep
- MsgBox (s)
-
- End Sub
-
- Function str2dec (strval As String) As Currency
- If Len(strval) = 6 Then
- str2dec = Val(Left$(strval, 2)) + Val(Mid$(strval, 3, 2)) / 60 + Val(Right$(strval, 2)) / 360
- ElseIf Len(strval) = 7 Then
- str2dec = Val(Left$(strval, 3)) + Val(Mid$(strval, 4, 2)) / 60 + Val(Right$(strval, 2)) / 360
- Else
- str2dec = 0
- End If
-
- End Function
-
-