home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch08code / address.frm < prev    next >
Text File  |  1994-09-19  |  13KB  |  431 lines

  1. VERSION 4.00
  2. Begin VB.Form Address 
  3.    Caption         =   "Sample Address Book Application"
  4.    ClientHeight    =   5235
  5.    ClientLeft      =   1365
  6.    ClientTop       =   1500
  7.    ClientWidth     =   8325
  8.    BeginProperty Font 
  9.       name            =   "MS Sans Serif"
  10.       charset         =   0
  11.       weight          =   700
  12.       size            =   8.25
  13.       underline       =   0   'False
  14.       italic          =   0   'False
  15.       strikethrough   =   0   'False
  16.    EndProperty
  17.    Height          =   5640
  18.    Left            =   1305
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   5235
  21.    ScaleWidth      =   8325
  22.    Top             =   1155
  23.    Width           =   8445
  24.    Begin VB.CommandButton ImportDbase 
  25.       Caption         =   "Import Dbase"
  26.       Height          =   255
  27.       Left            =   120
  28.       TabIndex        =   26
  29.       Top             =   3960
  30.       Width           =   1935
  31.    End
  32.    Begin VB.TextBox AddressCtl 
  33.       Height          =   375
  34.       Index           =   1
  35.       Left            =   1320
  36.       TabIndex        =   25
  37.       Top             =   120
  38.       Width           =   3375
  39.    End
  40.    Begin VB.CommandButton Command1 
  41.       Caption         =   "Clear"
  42.       Height          =   615
  43.       Left            =   2280
  44.       TabIndex        =   1
  45.       Top             =   4440
  46.       Width           =   1095
  47.    End
  48.    Begin VB.CommandButton FindPrevious 
  49.       Caption         =   "Find Previous"
  50.       Height          =   615
  51.       Left            =   6840
  52.       TabIndex        =   24
  53.       Top             =   4440
  54.       Width           =   1335
  55.    End
  56.    Begin VB.CommandButton FindNext 
  57.       Caption         =   "Find Next"
  58.       Height          =   615
  59.       Left            =   5520
  60.       TabIndex        =   23
  61.       Top             =   4440
  62.       Width           =   1335
  63.    End
  64.    Begin VB.CommandButton FindFirst 
  65.       Caption         =   "Find First"
  66.       Height          =   615
  67.       Left            =   4200
  68.       TabIndex        =   22
  69.       Top             =   4440
  70.       Width           =   1335
  71.    End
  72.    Begin VB.CommandButton Delete 
  73.       Caption         =   "Delete"
  74.       Height          =   615
  75.       Left            =   1200
  76.       TabIndex        =   21
  77.       Top             =   4440
  78.       Width           =   1095
  79.    End
  80.    Begin VB.CommandButton Add 
  81.       Caption         =   "Add"
  82.       Height          =   615
  83.       Left            =   120
  84.       TabIndex        =   20
  85.       Top             =   4440
  86.       Width           =   1095
  87.    End
  88.    Begin VB.TextBox AddressCtl 
  89.       Height          =   975
  90.       Index           =   10
  91.       Left            =   960
  92.       TabIndex        =   19
  93.       Top             =   2640
  94.       Width           =   6735
  95.    End
  96.    Begin VB.TextBox AddressCtl 
  97.       Height          =   375
  98.       Index           =   9
  99.       Left            =   6120
  100.       TabIndex        =   18
  101.       Top             =   2160
  102.       Width           =   1935
  103.    End
  104.    Begin VB.TextBox AddressCtl 
  105.       Height          =   375
  106.       Index           =   8
  107.       Left            =   3840
  108.       TabIndex        =   17
  109.       Top             =   2160
  110.       Width           =   1455
  111.    End
  112.    Begin VB.TextBox AddressCtl 
  113.       Height          =   375
  114.       Index           =   7
  115.       Left            =   960
  116.       TabIndex        =   16
  117.       Top             =   2160
  118.       Width           =   1575
  119.    End
  120.    Begin VB.TextBox AddressCtl 
  121.       Height          =   375
  122.       Index           =   6
  123.       Left            =   4920
  124.       TabIndex        =   15
  125.       Top             =   1560
  126.       Width           =   1935
  127.    End
  128.    Begin VB.TextBox AddressCtl 
  129.       Height          =   375
  130.       Index           =   5
  131.       Left            =   3120
  132.       TabIndex        =   14
  133.       Top             =   1560
  134.       Width           =   615
  135.    End
  136.    Begin VB.TextBox AddressCtl 
  137.       Height          =   375
  138.       Index           =   4
  139.       Left            =   840
  140.       TabIndex        =   13
  141.       Top             =   1560
  142.       Width           =   1575
  143.    End
  144.    Begin VB.TextBox AddressCtl 
  145.       Height          =   375
  146.       Index           =   3
  147.       Left            =   1320
  148.       TabIndex        =   12
  149.       Top             =   1080
  150.       Width           =   3375
  151.    End
  152.    Begin VB.TextBox AddressCtl 
  153.       Height          =   375
  154.       Index           =   2
  155.       Left            =   1320
  156.       TabIndex        =   11
  157.       Top             =   600
  158.       Width           =   3375
  159.    End
  160.    Begin VB.Label Label11 
  161.       Caption         =   "Notes:"
  162.       Height          =   255
  163.       Left            =   240
  164.       TabIndex        =   10
  165.       Top             =   2760
  166.       Width           =   615
  167.    End
  168.    Begin VB.Label Label10 
  169.       Caption         =   "Work Phone:"
  170.       Height          =   255
  171.       Left            =   2640
  172.       TabIndex        =   9
  173.       Top             =   2280
  174.       Width           =   1215
  175.    End
  176.    Begin VB.Label Label9 
  177.       Caption         =   "Fax:"
  178.       Height          =   255
  179.       Left            =   5400
  180.       TabIndex        =   8
  181.       Top             =   2280
  182.       Width           =   495
  183.    End
  184.    Begin VB.Label Label8 
  185.       Caption         =   "Phone:"
  186.       Height          =   255
  187.       Left            =   240
  188.       TabIndex        =   7
  189.       Top             =   2280
  190.       Width           =   735
  191.    End
  192.    Begin VB.Label Label7 
  193.       Caption         =   "Zip Code:"
  194.       Height          =   255
  195.       Left            =   3840
  196.       TabIndex        =   6
  197.       Top             =   1680
  198.       Width           =   975
  199.    End
  200.    Begin VB.Label Label6 
  201.       Caption         =   "State:"
  202.       Height          =   255
  203.       Left            =   2520
  204.       TabIndex        =   5
  205.       Top             =   1680
  206.       Width           =   615
  207.    End
  208.    Begin VB.Label Label5 
  209.       Caption         =   "City:"
  210.       Height          =   255
  211.       Left            =   240
  212.       TabIndex        =   4
  213.       Top             =   1680
  214.       Width           =   495
  215.    End
  216.    Begin VB.Label Label4 
  217.       Caption         =   "Street 2:"
  218.       Height          =   255
  219.       Left            =   240
  220.       TabIndex        =   3
  221.       Top             =   1200
  222.       Width           =   975
  223.    End
  224.    Begin VB.Label Label3 
  225.       Caption         =   "Street:"
  226.       Height          =   255
  227.       Left            =   240
  228.       TabIndex        =   2
  229.       Top             =   720
  230.       Width           =   975
  231.    End
  232.    Begin VB.Label Label1 
  233.       Caption         =   "Name:"
  234.       Height          =   255
  235.       Left            =   240
  236.       TabIndex        =   0
  237.       Top             =   240
  238.       Width           =   975
  239.    End
  240. End
  241. Attribute VB_Name = "Address"
  242. Attribute VB_Creatable = False
  243. Attribute VB_Exposed = False
  244.     ' Since all fields are in a control array
  245. ' I will setup defines for accessing them
  246. Const NAME_FLD = 1
  247. Const STREET_FLD = 2
  248. Const STREET2_FLD = 3
  249. Const CITY_FLD = 4
  250. Const STATE_FLD = 5
  251. Const ZIP_FLD = 6
  252. Const PHONE_FLD = 7
  253. Const WORK_FLD = 8
  254. Const FAX_FLD = 9
  255. Const NOTES_FLD = 10
  256. Const FIRST_FLD = NAME_FLD
  257. Const LAST_FLD = NOTES_FLD
  258.  
  259. ' Objects and variables global to the application
  260. Dim Ws As Workspace
  261. Dim Db As Database
  262. Dim Tbl As Recordset
  263. Dim SearchSet As Recordset
  264. Dim MemoSet As Recordset
  265. Dim AddInfoAdd As Recordset
  266. Dim SrchValue As String
  267.  
  268. Private Sub Add_Click()
  269.     If Not AddressCtl(NAME_FLD) = "" Then
  270.         Tbl.AddNew
  271.         Tbl.Fields("Name").Value = AddressCtl(NAME_FLD)
  272.         Tbl.Fields("Street").Value = AddressCtl(STREET_FLD)
  273.         Tbl.Fields("Street2").Value = AddressCtl(STREET2_FLD)
  274.         Tbl.Fields("City").Value = AddressCtl(CITY_FLD)
  275.         Tbl.Fields("State").Value = AddressCtl(STATE_FLD)
  276.         Tbl.Fields("ZipCode").Value = AddressCtl(ZIP_FLD)
  277.         Tbl.Fields("Phone").Value = AddressCtl(PHONE_FLD)
  278.         Tbl.Fields("WorkPhone").Value = AddressCtl(WORK_FLD)
  279.         Tbl.Fields("Fax").Value = AddressCtl(FAX_FLD)
  280.         Tbl.Update
  281.         'Tbl.Fields("Notes").Value = AddressCtl(NOTES_FLD)
  282.         'Tbl.Update
  283.  
  284.         AddInfoAdd.AddNew
  285.         AddInfoAdd.Fields("Name").Value = AddressCtl(NAME_FLD)
  286.         AddInfoAdd.Fields("Notes").Value = AddressCtl(NOTES_FLD)
  287.         AddInfoAdd.Update
  288.     End If
  289. End Sub
  290.  
  291. Private Sub Command1_Click()
  292.     Dim i As Integer
  293.     For i = FIRST_FLD To LAST_FLD Step 1
  294.         AddressCtl(i) = ""
  295.     Next i
  296. End Sub
  297.  
  298. Private Sub Delete_Click()
  299.     If Not SearchSet.NoMatch Then
  300.        SearchSet.Delete
  301.        If Not MemoSet.NoMatch Then
  302.         MemoSet.Delete
  303.        End If
  304.        Command1_Click
  305.        FindFirst_Click
  306.     End If
  307. End Sub
  308.  
  309. Private Sub FillFormfromImport()
  310.     If Not SearchSet.NoMatch Then
  311.         AddressCtl(NAME_FLD) = ValidateRecordField("Name")
  312.         AddressCtl(STREET_FLD) = ValidateRecordField("Street")
  313.         AddressCtl(STREET2_FLD) = ValidateRecordField("Street2")
  314.         AddressCtl(CITY_FLD) = ValidateRecordField("City")
  315.         AddressCtl(STATE_FLD) = ValidateRecordField("State")
  316.         AddressCtl(ZIP_FLD) = ValidateRecordField("ZipCode")
  317.         AddressCtl(PHONE_FLD) = ValidateRecordField("Phone")
  318.         AddressCtl(WORK_FLD) = ValidateRecordField("WorkPhone")
  319.         AddressCtl(FAX_FLD) = ValidateRecordField("Fax")
  320.         AddressCtl(NOTES_FLD) = ValidateRecordField("Notes")
  321.     End If
  322.  
  323. End Sub
  324.  
  325. Private Sub FillFormfromRecord()
  326.     If Not SearchSet.NoMatch Then
  327.         AddressCtl(NAME_FLD) = ValidateRecordField("Name")
  328.         AddressCtl(STREET_FLD) = ValidateRecordField("Street")
  329.         AddressCtl(STREET2_FLD) = ValidateRecordField("Street2")
  330.         AddressCtl(CITY_FLD) = ValidateRecordField("City")
  331.         AddressCtl(STATE_FLD) = ValidateRecordField("State")
  332.         AddressCtl(ZIP_FLD) = ValidateRecordField("ZipCode")
  333.         AddressCtl(PHONE_FLD) = ValidateRecordField("Phone")
  334.         AddressCtl(WORK_FLD) = ValidateRecordField("WorkPhone")
  335.         AddressCtl(FAX_FLD) = ValidateRecordField("Fax")
  336.  
  337.         Dim AttachStatement As String
  338.         AttachStatement = "SELECT * FROM AddInfo WHERE Name = '" + AddressCtl(NAME_FLD) + "'"
  339.         Set MemoSet = Db.OpenRecordSet(AttachStatement, dbOpenDynaset)
  340.         If MemoSet.BOF = False Then
  341.             If MemoSet.Fields("Notes").Value > "" Then
  342.                 AddressCtl(NOTES_FLD) = MemoSet.Fields("Notes").Value
  343.             Else
  344.                 AddressCtl(NOTES_FLD) = ""
  345.             End If
  346.         End If
  347.     End If
  348. End Sub
  349.  
  350. Private Sub FindFirst_Click()
  351.     Dim Statement As String
  352.     Statement = "SELECT * FROM Address WHERE Name >= '" + AddressCtl(NAME_FLD) + "'"
  353.     Set SearchSet = Db.OpenRecordSet(Statement, dbOpenDynaset)
  354.     FillFormfromRecord
  355. End Sub
  356.  
  357. Private Sub FindNext_Click()
  358.     SearchSet.FindNext "Name > ' '"
  359.     FillFormfromRecord
  360. End Sub
  361.  
  362. Private Sub FindPrevious_Click()
  363.     SearchSet.FindPrevious "Name > ' '"
  364.     FillFormfromRecord
  365. End Sub
  366.  
  367. Private Sub Form_Load()
  368.     Dim TblDef As New TableDef
  369.  
  370.     ' The first step is to create a default workspace to use for the app
  371.     Set Ws = DBEngine.Workspaces(0)
  372.     ' second open the access database
  373.     Set Db = Ws.OpenDatabase("\vb4\address\address.mdb")
  374.  
  375.     ' now attach the dbase IV table to the open db
  376.     TblDef.Connect = "dBASE IV;DATABASE=\VB4\ADDress"
  377.     TblDef.SourceTableName = "ADDINFO"   ' The name of the file.
  378.     TblDef.Name = "AddInfo"    ' The name in your database.
  379.     Db.TableDefs.Append TblDef  ' Create the link.
  380.  
  381.     ' now open a table info the main database
  382.     Set Tbl = Db.OpenRecordSet("Address", dbOpenTable)
  383.  
  384.     ' now we need to create a dynaset from the attached table
  385.     Statement = "SELECT * FROM Address WHERE Name >= '" + AddressCtl(NAME_FLD) + "'"
  386.     Set AddInfoAdd = Db.OpenRecordSet("SELECT * FROM AddInfo", dbOpenDynaset)
  387. End Sub
  388.  
  389.  
  390. Private Sub Form_Unload(Cancel As Integer)
  391.     Tbl.Close
  392.     AddInfoAdd.Close
  393.     Db.TableDefs.Delete "AddInfo"
  394. End Sub
  395.  
  396.  
  397.  
  398.  
  399. Private Sub ImportDbase_Click()
  400.     Dim dbdb As Database
  401.     Dim DTable As Recordset
  402.     Dim Statement As String
  403.  
  404.     Set dbdb = Ws.OpenDatabase("\vb4\address", False, False, "dBase IV")
  405.     Set DTable = dbdb.OpenRecordSet("Address", dbOpenTable)
  406.  
  407.     Statement = "SELECT * FROM Address"
  408.     Set SearchSet = dbdb.OpenRecordSet(Statement, dbOpenDynaset)
  409.     While Not SearchSet.EOF
  410.         FillFormfromImport
  411.         Add_Click
  412.         Command1_Click
  413.         SearchSet.MoveNext
  414.     Wend
  415.     dbdb.Close
  416.     DTable.Close
  417. End Sub
  418.  
  419. Private Function ValidateRecordField(Field As String) As String
  420.     If SearchSet.BOF = False Then
  421.         If SearchSet.Fields(Field).Value > "" Then
  422.             ValidateRecordField = SearchSet.Fields(Field).Value
  423.         Else
  424.             ValidateRecordField = ""
  425.         End If
  426.     End If
  427. End Function
  428.  
  429.  
  430.  
  431.