home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD141651252001.psc / frmContacts.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-01-25  |  28.8 KB  |  796 lines

  1. VERSION 5.00
  2. Begin VB.Form frmContacts 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Postal Mailing List Maintenance"
  5.    ClientHeight    =   5910
  6.    ClientLeft      =   45
  7.    ClientTop       =   615
  8.    ClientWidth     =   8910
  9.    Icon            =   "frmContacts.frx":0000
  10.    KeyPreview      =   -1  'True
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   5910
  15.    ScaleWidth      =   8910
  16.    ShowInTaskbar   =   0   'False
  17.    Begin VB.Frame Frame1 
  18.       Height          =   5760
  19.       Left            =   255
  20.       TabIndex        =   14
  21.       Top             =   -15
  22.       Width           =   8415
  23.       Begin VB.CommandButton cmdUpdateBatch 
  24.          Caption         =   "Reconnect and Update Database"
  25.          Height          =   435
  26.          Left            =   180
  27.          TabIndex        =   16
  28.          Top             =   5280
  29.          Width           =   1905
  30.       End
  31.       Begin VB.CommandButton cmdButton 
  32.          Caption         =   ">>|"
  33.          Height          =   480
  34.          Index           =   9
  35.          Left            =   5865
  36.          TabIndex        =   17
  37.          Top             =   4170
  38.          Width           =   1005
  39.       End
  40.       Begin VB.CommandButton cmdButton 
  41.          Caption         =   ">"
  42.          Height          =   480
  43.          Index           =   8
  44.          Left            =   4845
  45.          TabIndex        =   15
  46.          Top             =   4170
  47.          Width           =   1005
  48.       End
  49.       Begin VB.CommandButton cmdButton 
  50.          Caption         =   "<"
  51.          Height          =   480
  52.          Index           =   7
  53.          Left            =   3840
  54.          TabIndex        =   13
  55.          Top             =   4170
  56.          Width           =   1005
  57.       End
  58.       Begin VB.CommandButton cmdButton 
  59.          Caption         =   "|<<"
  60.          Height          =   480
  61.          Index           =   6
  62.          Left            =   2835
  63.          TabIndex        =   12
  64.          Top             =   4170
  65.          Width           =   1005
  66.       End
  67.       Begin VB.CommandButton cmdButton 
  68.          Caption         =   "&Exit"
  69.          Height          =   480
  70.          Index           =   5
  71.          Left            =   6900
  72.          TabIndex        =   11
  73.          Top             =   3540
  74.          Width           =   1005
  75.       End
  76.       Begin VB.CommandButton cmdButton 
  77.          Caption         =   "&Add New"
  78.          Height          =   480
  79.          Index           =   4
  80.          Left            =   5886
  81.          TabIndex        =   10
  82.          Top             =   3540
  83.          Width           =   1005
  84.       End
  85.       Begin VB.CommandButton cmdButton 
  86.          Caption         =   "&Save"
  87.          Height          =   480
  88.          Index           =   3
  89.          Left            =   4872
  90.          TabIndex        =   9
  91.          ToolTipText     =   "Changes in a disconnected recordset are not saved to file until .UpdateBatch"
  92.          Top             =   3540
  93.          Width           =   1005
  94.       End
  95.       Begin VB.CommandButton cmdButton 
  96.          Caption         =   "&Edit"
  97.          Height          =   480
  98.          Index           =   2
  99.          Left            =   3858
  100.          TabIndex        =   8
  101.          Top             =   3540
  102.          Width           =   1005
  103.       End
  104.       Begin VB.CommandButton cmdButton 
  105.          Caption         =   "&Cancel"
  106.          Height          =   480
  107.          Index           =   1
  108.          Left            =   2844
  109.          TabIndex        =   7
  110.          Top             =   3540
  111.          Width           =   1005
  112.       End
  113.       Begin VB.CommandButton cmdButton 
  114.          Caption         =   "&Delete"
  115.          Height          =   480
  116.          Index           =   0
  117.          Left            =   1830
  118.          TabIndex        =   6
  119.          Top             =   3540
  120.          Width           =   1005
  121.       End
  122.       Begin VB.ComboBox cboContactName 
  123.          Height          =   315
  124.          Left            =   1845
  125.          TabIndex        =   0
  126.          Text            =   "(enter contact person's name)"
  127.          Top             =   915
  128.          Width           =   6135
  129.       End
  130.       Begin VB.TextBox txtState 
  131.          Height          =   315
  132.          Left            =   1830
  133.          TabIndex        =   4
  134.          Text            =   "(enter State)"
  135.          Top             =   2820
  136.          Width           =   2175
  137.       End
  138.       Begin VB.TextBox txtAddressLine1 
  139.          Height          =   315
  140.          Left            =   1830
  141.          TabIndex        =   2
  142.          Text            =   "(number and street)"
  143.          Top             =   1860
  144.          Width           =   6135
  145.       End
  146.       Begin VB.TextBox txtCity 
  147.          Height          =   315
  148.          Left            =   1830
  149.          TabIndex        =   3
  150.          Text            =   "(enter city name)"
  151.          Top             =   2325
  152.          Width           =   6135
  153.       End
  154.       Begin VB.TextBox txtZipCode 
  155.          Height          =   315
  156.          Left            =   5790
  157.          TabIndex        =   5
  158.          Text            =   "(enter zip code)"
  159.          Top             =   2820
  160.          Width           =   2175
  161.       End
  162.       Begin VB.TextBox txtCompany 
  163.          Height          =   315
  164.          Left            =   1830
  165.          TabIndex        =   1
  166.          Text            =   "(enter your dba, if any)"
  167.          Top             =   1380
  168.          Width           =   6135
  169.       End
  170.       Begin VB.Label Label3 
  171.          Appearance      =   0  'Flat
  172.          BackColor       =   &H00C0C0C0&
  173.          BorderStyle     =   1  'Fixed Single
  174.          Caption         =   "Mailing List Maintenance"
  175.          BeginProperty Font 
  176.             Name            =   "MS Sans Serif"
  177.             Size            =   12
  178.             Charset         =   0
  179.             Weight          =   700
  180.             Underline       =   0   'False
  181.             Italic          =   0   'False
  182.             Strikethrough   =   0   'False
  183.          EndProperty
  184.          ForeColor       =   &H80000008&
  185.          Height          =   420
  186.          Left            =   285
  187.          TabIndex        =   26
  188.          Top             =   225
  189.          Width           =   3150
  190.       End
  191.       Begin VB.Label Label2 
  192.          Appearance      =   0  'Flat
  193.          BackColor       =   &H80000005&
  194.          BorderStyle     =   1  'Fixed Single
  195.          Caption         =   "<-- Click Button to Reconnect and do Batch Update on the Disconnected Recordset."
  196.          ForeColor       =   &H80000008&
  197.          Height          =   330
  198.          Left            =   2175
  199.          TabIndex        =   25
  200.          Top             =   5295
  201.          Width           =   6195
  202.       End
  203.       Begin VB.Label Label1 
  204.          Appearance      =   0  'Flat
  205.          BackColor       =   &H80000005&
  206.          BorderStyle     =   1  'Fixed Single
  207.          Caption         =   "Status: Recordset is now Disconnected from .mdb file to free up open files and network connection if applicable. "
  208.          ForeColor       =   &H80000008&
  209.          Height          =   330
  210.          Left            =   135
  211.          TabIndex        =   24
  212.          Top             =   4890
  213.          Width           =   8265
  214.       End
  215.       Begin VB.Label lblZip 
  216.          Caption         =   "Zip Code:"
  217.          Height          =   255
  218.          Left            =   4545
  219.          TabIndex        =   23
  220.          Top             =   2865
  221.          Width           =   975
  222.       End
  223.       Begin VB.Label lblContactName 
  224.          Caption         =   "Contact Name:"
  225.          Height          =   255
  226.          Left            =   375
  227.          TabIndex        =   22
  228.          Top             =   960
  229.          Width           =   1335
  230.       End
  231.       Begin VB.Label lblStreetAddress 
  232.          Caption         =   "Street Address:"
  233.          Height          =   255
  234.          Left            =   375
  235.          TabIndex        =   21
  236.          Top             =   1920
  237.          Width           =   1335
  238.       End
  239.       Begin VB.Label lblCity 
  240.          Caption         =   "City:"
  241.          Height          =   255
  242.          Left            =   375
  243.          TabIndex        =   20
  244.          Top             =   2385
  245.          Width           =   1335
  246.       End
  247.       Begin VB.Label lblCompany 
  248.          Caption         =   "Company Name:"
  249.          Height          =   255
  250.          Left            =   375
  251.          TabIndex        =   19
  252.          Top             =   1410
  253.          Width           =   1335
  254.       End
  255.       Begin VB.Label lblState 
  256.          Caption         =   "State:"
  257.          Height          =   255
  258.          Left            =   375
  259.          TabIndex        =   18
  260.          Top             =   2865
  261.          Width           =   1335
  262.       End
  263.    End
  264.    Begin VB.Menu mnuAbout 
  265.       Caption         =   "&About"
  266.    End
  267. Attribute VB_Name = "frmContacts"
  268. Attribute VB_GlobalNameSpace = False
  269. Attribute VB_Creatable = False
  270. Attribute VB_PredeclaredId = True
  271. Attribute VB_Exposed = False
  272. Option Explicit
  273. 'Included as Project References
  274. 'Microsoft Scripting Runtime
  275. 'Microsoft ADO Ext. for DDL and Security
  276. 'Microsoft ActiveX Data Object 2.x Library
  277. 'Microsoft DataBinding collection
  278. 'Original codes by Legrev3@aol.com
  279. 'ADOX database creation, ADO databinding, Disconnected Recordsets demo
  280. 'Submitted for downloading Jan. 24, 2001
  281. Dim cnContacts As ADODB.Connection
  282. Dim rsContacts As ADODB.Recordset
  283. Dim OneRec As UserDefRec
  284. Dim blnAdd As Boolean           'set to true if adding
  285. Dim blnEdit As Boolean          'set to true if editing
  286. Dim blnUpdated As Boolean       'set to true if modifcations are written to database
  287. Dim blnExiting As Boolean       'set to true if closing app
  288. Dim strSearch As String
  289. Dim strEditRec As String
  290. Dim vntBookMark As Variant
  291. Dim intResponse As Integer
  292. Private Enum cmdButtons
  293.     DeleteButton = 0
  294.     CancelButton = 1
  295.     EditButton = 2
  296.     SaveButton = 3
  297.     AddNewButton = 4
  298.     ExitButton = 5
  299.     MoveFirstButton = 6
  300.     MovePreviousButton = 7
  301.     MoveNextButton = 8
  302.     MoveLastButton = 9
  303. End Enum
  304. Private Sub Form_Load()
  305.     Dim blnFileExists As Boolean
  306.     Dim strDbName As String
  307.     strDbName = "Contacts.mdb"                      'database name to use
  308.     'strFilespec and strConn are declared in modCreateDatabase.bas
  309.     'in a client-server environment, the db path would be on the network server
  310.     'using an ODBC compliant provider.
  311.     'this demo was modified to run on a stand-alone computer.
  312.     strFilespec = App.Path & "\Data\" & strDbName
  313.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilespec & ";"
  314.     blnFileExists = DatabaseExist()        'fn to see if Contacts.mdb exist
  315.     If blnFileExists = False Then
  316.         intResponse = MsgBox(strFilespec & " does not exist and will now be created.", vbInformation + vbOKCancel)
  317.         If intResponse = vbCancel Then Unload Me
  318.         Call CreateDataFile                'if not exist create .mdb
  319.         Call CreateMailListTable                    'create table MailList
  320.     End If
  321.     Set cnContacts = New ADODB.Connection       'use ADO connection
  322.     Set rsContacts = New ADODB.Recordset        'and ADO recordset
  323.     If cnContacts.State = adStateClosed Then    'newly-created db may be open so
  324.         cnContacts.CursorLocation = adUseClient 'create client side
  325.         cnContacts.Open strConn                 'check first before opening
  326.     End If
  327.     rsContacts.Open "SELECT * FROM MailList ORDER BY ContactName", cnContacts, adOpenStatic, adLockBatchOptimistic, adCmdText
  328.         
  329.     'populate combo box with names
  330.     Do Until rsContacts.EOF
  331.         If Not IsNull(rsContacts!ContactName) Then cboContactName.AddItem rsContacts!ContactName
  332.         rsContacts.MoveNext
  333.     Loop
  334.     'data binding - do not bind cboContactName to enable search ability
  335.     Set txtCompany.DataSource = rsContacts
  336.     txtCompany.DataField = "CompanyName"
  337.     Set txtAddressLine1.DataSource = rsContacts
  338.     txtAddressLine1.DataField = "AddressLine1"
  339.     Set txtCity.DataSource = rsContacts
  340.     txtCity.DataField = "City"
  341.     Set txtState.DataSource = rsContacts
  342.     txtState.DataField = "State"
  343.     Set txtZipCode.DataSource = rsContacts
  344.     txtZipCode.DataField = "ZipCode"
  345.     If cboContactName.ListCount > 0 Then
  346.         rsContacts.MoveFirst
  347.         cboContactName.Text = rsContacts!ContactName
  348.     End If
  349.     DoEvents
  350.     'Create a disconnected recordset
  351.     'after getting the records, the client now disconnects.
  352.     'this is the essence of disconnected recordsets
  353.     Set rsContacts.ActiveConnection = Nothing
  354.     cnContacts.Close
  355.     blnUpdated = True       'flag the fact that batch update is not yet necessary
  356. End Sub
  357. Private Sub cmdButton_Click(Index As Integer)
  358. 'determine which button is clicked and do appropriate action
  359.     Select Case Index
  360.         Case DeleteButton
  361.             Dim strTemp As String
  362.             Dim i As Integer
  363.             
  364.             If cboContactName.ListCount = 1 Then
  365.                 MsgBox "Deleting ALL records will cause an Update error." & vbCr & _
  366.                         "Sorry, last record will not be deleted in this simulation.", vbCritical + vbOKOnly
  367.                 Exit Sub
  368.             End If
  369.             
  370.             If rsContacts.EOF Or rsContacts.BOF Then
  371.                 MsgBox "There is no record to delete.", vbExclamation + vbOKOnly
  372.                 Exit Sub
  373.             End If
  374.             
  375.             intResponse = MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo)
  376.             If intResponse = vbNo Then Exit Sub
  377.             
  378.             strTemp = Trim$(cboContactName.Text)
  379.             
  380.             On Error GoTo DeleteError:
  381.             rsContacts.Delete
  382.             On Error GoTo 0
  383.                         
  384.             For i = 0 To cboContactName.ListCount
  385.                 If cboContactName.List(i) = strTemp Then
  386.                     cboContactName.RemoveItem i
  387.                     Exit For
  388.                 End If
  389.             Next i
  390.                             
  391.             rsContacts.MoveNext
  392.             If rsContacts.EOF Then rsContacts.MoveFirst
  393.             
  394.             blnUpdated = False   'we have something to write to db from disconnected recordset
  395.             cboContactName.Text = rsContacts!ContactName
  396.             cboContactName.SetFocus
  397.             DoEvents
  398.                         
  399.         Case CancelButton
  400.             If blnEdit = False And blnAdd = False Then
  401.                 MsgBox "There is no Edit or Add activity to Cancel.", vbExclamation
  402.                 Call EnableButtons("YYYYYYYYYY")
  403.                 Exit Sub
  404.             End If
  405.             
  406.             Call EnableButtons("YYYYYYYYYY")
  407.             
  408.             If blnEdit = True Then
  409.                 Call ClearText
  410.                 Call RestoreCurrent
  411.                 cboContactName = rsContacts!ContactName
  412.                 blnEdit = False
  413.                 cboContactName.SetFocus
  414.                 Exit Sub
  415.             End If
  416.             
  417.             
  418.             On Error Resume Next
  419.             
  420.             rsContacts.CancelUpdate
  421.             If vntBookMark <> "" Then rsContacts.Bookmark = vntBookMark
  422.             vntBookMark = ""
  423.             cboContactName.Text = rsContacts!ContactName
  424.             cboContactName.SetFocus
  425.             blnAdd = False
  426.             On Error GoTo 0
  427.             DoEvents
  428.                         
  429.         Case EditButton
  430.             If cboContactName.ListCount = 0 Then
  431.                 MsgBox "Contacts tables is empty."
  432.                 cboContactName.SetFocus
  433.                 Exit Sub
  434.             End If
  435.             
  436.             blnEdit = True                  'set blnEdit flag - we are editing
  437.             
  438.             Call SaveCurrent                'in case of cancel
  439.             
  440.             strEditRec = cboContactName
  441.             Call EnableButtons("NYNYNNNNNN")        'set applicable buttons to Y
  442.             cboContactName.Text = rsContacts!ContactName
  443.             txtCompany.SetFocus
  444.             
  445.         Case SaveButton
  446.             If blnAdd = False And blnEdit = False Then
  447.                 MsgBox "There is no edited or added record to save.", vbExclamation
  448.                 Exit Sub
  449.             End If
  450.             
  451.             Call EnableButtons("YYYYYYYYYY")        'all buttons enabled
  452.             
  453.             If blnEdit = True Then                  'Edit was clicked so
  454.                 blnEdit = False                     'reset blnEdit flag
  455.                 If strEditRec <> cboContactName Then
  456.                     MsgBox "ContactName is a primary field and may not be edited.", vbExclamation + vbOKOnly
  457.                     Call RestoreCurrent
  458.                     cboContactName = rsContacts!ContactName
  459.                     Exit Sub
  460.                 End If
  461.             Else
  462.                 rsContacts!ContactName = cboContactName.Text
  463.                 rsContacts.Update
  464.                 rsContacts.MoveLast                         'added new record
  465.                 cboContactName.AddItem cboContactName.Text  'update combo box
  466.                 blnAdd = False                              'reset blnAdd flag
  467.             End If
  468.             On Error GoTo 0
  469.             
  470.             blnUpdated = False   'we have something to write to db from disconnected recordset
  471.             
  472.         Case AddNewButton
  473.             blnAdd = True                       'set blnAdd flag - we are adding
  474.             vntBookMark = ""
  475.             On Error Resume Next
  476.             vntBookMark = rsContacts.Bookmark   'in case Cancel is clicked
  477.             rsContacts.AddNew
  478.             Call EnableButtons("NYNYNNNNNN")
  479.             Call ClearText
  480.             cboContactName.SetFocus
  481.             On Error GoTo 0
  482.             
  483.         Case ExitButton
  484.             Unload Me
  485.             
  486. 'Navigation buttons
  487.         Case MoveFirstButton
  488.             If cboContactName.ListCount = 0 Then
  489.                 MsgBox "Contacts tables is empty.", vbExclamation
  490.             Else
  491.                 rsContacts.MoveFirst
  492.                 cboContactName.Text = rsContacts!ContactName
  493.             End If
  494.             
  495.         Case MovePreviousButton
  496.             If cboContactName.ListCount = 0 Then
  497.                 MsgBox "Contacts tables is empty.", vbExclamation
  498.             Else
  499.                 rsContacts.MovePrevious
  500.                 If rsContacts.BOF Then
  501.                     rsContacts.MoveFirst
  502.                     MsgBox "The current record is the first on file."
  503.                 End If
  504.                 cboContactName.Text = rsContacts!ContactName
  505.             End If
  506.             
  507.         Case MoveNextButton
  508.             If cboContactName.ListCount = 0 Then
  509.                 MsgBox "Contacts tables is empty.", vbExclamation
  510.             Else
  511.                 rsContacts.MoveNext
  512.                 If rsContacts.EOF Then
  513.                     rsContacts.MoveLast
  514.                     MsgBox "The current record is the last on file."
  515.                 End If
  516.                 cboContactName.Text = rsContacts!ContactName
  517.             End If
  518.             
  519.         Case MoveLastButton
  520.             If cboContactName.ListCount = 0 Then
  521.                 MsgBox "Contacts tables is empty.", vbExclamation
  522.             Else
  523.                 rsContacts.MoveLast
  524.                 cboContactName.Text = rsContacts!ContactName
  525.             End If
  526.     End Select
  527. Exit Sub
  528. DeleteError:
  529.     MsgBox "There has been a delete error. If record is not deleted try again later."
  530. End Sub
  531. Private Sub Form_KeyPress(KeyAscii As Integer)
  532.     If blnEdit = False And blnAdd = False Then
  533.         MsgBox "Click Edit to edit records or AddNew to enter new records.", vbOKOnly + vbInformation
  534.         KeyAscii = 0
  535.     End If
  536. End Sub
  537. Public Sub EnableButtons(strYN As String)
  538. 'parameter Y to enable no to disable cmdButton(0) to (10)
  539. 'position of Y or N on strYN corresponds to button index
  540.     Dim intIndex As Integer
  541.     Dim intAllButtons As Integer
  542.     strYN = Trim$(strYN)
  543.     intAllButtons = Len(strYN)
  544.     For intIndex = 1 To intAllButtons
  545.         cmdButton(intIndex - 1).Enabled = True      'default is enabled
  546.         If (Mid$(strYN, intIndex, 1) = "N") Then cmdButton(intIndex - 1).Enabled = False
  547.     Next intIndex
  548. End Sub
  549. Public Sub ClearText()
  550. 'clears text of all bound controls - textboxes and combobox
  551.     Dim i As Integer
  552.     For i = 1 To Me.Controls.Count - 1
  553.         If (TypeOf Me.Controls(i) Is TextBox) Then
  554.             Me.Controls(i).Text = ""
  555.         ElseIf (TypeOf Me.Controls(i) Is ComboBox) Then
  556.             Me.Controls(i).Text = ""
  557.         End If
  558.     Next i
  559.     cboContactName.SetFocus
  560. End Sub
  561. Private Sub cboContactName_Click()
  562. 'fires when a combobox item is clicked or Enter is hit on the combobox
  563. 'determines if contact name already exist on file
  564. 'if not, then allow to add
  565.     If blnAdd = False Then                  'AddNew was not clicked
  566.         Dim blnFound As Boolean
  567.         strSearch = CStr(cboContactName.Text)
  568.         blnFound = FindRec()
  569.         
  570.         If blnFound = True Then             'contact name is on file, displayed
  571.             cboContactName.SetFocus
  572.             Exit Sub
  573.         End If
  574.     End If
  575. 'user clicked AddNew or ContactName is not on file
  576. 'by setting focus on txtCompany, we are triggering cboContactName_LostFocus()
  577.     txtCompany.SetFocus
  578. End Sub
  579. Private Sub cboContactName_KeyPress(KeyAscii As Integer)
  580. 'triggers cboContactName.LostFocus()
  581.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  582.     txtCompany.SetFocus
  583. End Sub
  584. Private Sub cboContactName_LostFocus()
  585. 'user leaves combobox, determine what is on it except for Cancel button
  586.     If Me.ActiveControl = cmdButton(CancelButton) Then Exit Sub
  587.     Dim intResponse As Integer
  588.     intResponse = Validate
  589.     If intResponse = 1 Then
  590.         cmdButton_Click (CancelButton)      'Cancel
  591.         Exit Sub
  592.     ElseIf intResponse = 2 Then
  593.         cboContactName.SetFocus
  594.         Exit Sub
  595.     End If
  596.     ' intResponse = 0 allow to proceed
  597.     If blnAdd = True Then
  598.         Dim i As Integer
  599.         Dim blnFound As Boolean
  600.         blnFound = False
  601.         strSearch = CStr(cboContactName.Text)
  602.         For i = 0 To cboContactName.ListCount
  603.             If cboContactName.List(i) = cboContactName.Text Then
  604.                 rsContacts.CancelUpdate
  605.                 rsContacts.MoveFirst
  606.                 blnAdd = False
  607.                 blnFound = FindRec()
  608.                 Exit For
  609.             End If
  610.         Next i
  611.         If blnFound = True Then
  612.             cboContactName.Text = rsContacts!ContactName
  613.             Call EnableButtons("YYYYYYYYYY")
  614.             MsgBox "Record Exists.", vbInformation + vbOKOnly
  615.             DoEvents
  616.             cboContactName.SetFocus
  617.             Exit Sub
  618.         End If
  619.     End If
  620. End Sub
  621. Public Sub SaveCurrent()
  622. 'save current record if edit is clicked
  623.     With OneRec
  624.         On Error Resume Next
  625.         .strContactName = CStr(rsContacts!ContactName) & ""
  626.         .strCompany = CStr(rsContacts!CompanyName) & ""
  627.         .strAddressLine1 = CStr(rsContacts!AddressLine1) & ""
  628.         .strCity = CStr(rsContacts!City) & ""
  629.         .strState = CStr(rsContacts!State) & ""
  630.         .strZipCode = CStr(rsContacts!ZipCode) & ""
  631.     End With
  632. End Sub
  633. Public Sub RestoreCurrent()
  634. 'restore current record if Cancel is clicked after edit
  635.     With rsContacts
  636.         On Error Resume Next
  637.         !ContactName = OneRec.strContactName
  638.         !CompanyName = OneRec.strCompany
  639.         !AddressLine1 = OneRec.strAddressLine1
  640.         !City = OneRec.strCity
  641.         !State = OneRec.strState
  642.         !ZipCode = OneRec.strZipCode
  643.     End With
  644. End Sub
  645. Private Sub cmdUpdateBatch_Click()
  646.     If blnUpdated = True Then
  647.         MsgBox "There are no modifications to save to database file.", vbInformation
  648.         cboContactName.SetFocus
  649.         Exit Sub
  650.     End If
  651.     cnContacts.Open                              'reopen connection
  652.     Set rsContacts.ActiveConnection = cnContacts 'reconnect recordset
  653.     rsContacts.UpdateBatch
  654.     If blnExiting = True Then                     'fired from QueryUnload
  655.         Set rsContacts.ActiveConnection = Nothing
  656.         cnContacts.Close
  657.         Exit Sub
  658.     End If
  659.     Label1 = "Status: Connection reopened and database updated. Closed after update."
  660.     MsgBox "Modifications to Disconnected Recordset has been saved." & vbCr & "Press OK to Close and recreate Disconnected Recordset."
  661.     'recreate recordset
  662.     rsContacts.Requery
  663.     'populate combo box with names
  664.     cboContactName.Clear
  665.     Do Until rsContacts.EOF
  666.         If Not IsNull(rsContacts!ContactName) Then cboContactName.AddItem rsContacts!ContactName
  667.         rsContacts.MoveNext
  668.     Loop
  669.     'close connection to create a disconnected recordset
  670.     Set rsContacts.ActiveConnection = Nothing
  671.     cnContacts.Close
  672.     'set updated flag
  673.     blnUpdated = True
  674.     blnAdd = False
  675.     blnEdit = False
  676.     rsContacts.MoveFirst
  677.     cboContactName.Text = rsContacts!ContactName
  678. End Sub
  679. Private Function Validate() As Integer
  680.     Dim x As Integer
  681.     Dim strMsg As String
  682.     If blnEdit = True Then
  683.         If strEditRec <> cboContactName.Text Then
  684.             strMsg = "The Contact Name field is a primary key and may not be edited."
  685.             GoTo CannotEditName:
  686.         Else
  687.             Validate = 0
  688.             Exit Function
  689.         End If
  690.     End If
  691.     'new record is being added
  692.     cboContactName.Text = Trim$(cboContactName.Text)
  693.     If Len(cboContactName.Text) = 0 Then
  694.         strMsg = "The ContactName field is the primary key and is required."
  695.         GoTo RequiredField:
  696.     End If
  697.         
  698.     Validate = 0
  699.     Exit Function
  700. CannotEditName:
  701. 'usual for the primary key is a CompanyId or uniqe identifier
  702. 'we use the contact name to somewhat shorten code
  703.     MsgBox strMsg, vbExclamation + vbOKOnly
  704.     Validate = 1
  705.     Exit Function
  706. RequiredField:
  707.     x = MsgBox(strMsg, vbRetryCancel)
  708.     If x = vbCancel Then
  709.         Validate = 1
  710.     Else
  711.         Validate = 2
  712.     End If
  713. End Function
  714. Public Function FindRec() As Boolean
  715.     Dim strTemp
  716.     strTemp = "'" & strSearch & "'"
  717.     On Error Resume Next
  718.     rsContacts.MoveFirst
  719.     On Error GoTo ErrorNotOnFile:
  720.     rsContacts.Find "ContactName = " & strTemp, 0, adSearchForward
  721.     If rsContacts!ContactName = strSearch Then FindRec = True       'found
  722.     On Error GoTo 0
  723.     Err.Clear
  724.     Exit Function
  725. ErrorNotOnFile:
  726.     MsgBox "Error =   " & Err.Number & Err.Description
  727.     FindRec = False      'not found
  728.     DoEvents
  729.     On Error GoTo 0
  730.     Err.Clear
  731. End Function
  732. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  733.     If blnUpdated = False Then
  734.         intResponse = MsgBox("Do you wish to send your modifications to the database file?", vbQuestion + vbYesNoCancel)
  735.         If intResponse = vbYes Then
  736.             blnExiting = True
  737.             Call cmdUpdateBatch_Click
  738.         ElseIf intResponse = vbNo Then
  739.             Exit Sub
  740.         ElseIf intResponse = vbCancel Then
  741.             Cancel = True
  742.         End If
  743.     End If
  744. End Sub
  745. Private Sub mnuAbout_Click()
  746.     frmAbout.Show
  747. End Sub
  748. 'these KeyPress Subs merely allow the enter key to behave like the tab key
  749. 'and are not essential in this demo
  750. 'the GotFocus codes merely highlight text and again are not essential
  751. Private Sub txtAddressLine1_KeyPress(KeyAscii As Integer)
  752.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  753.     txtAddressLine1 = Trim$(txtAddressLine1)
  754.     txtCity.SetFocus
  755. End Sub
  756. Private Sub txtAddressLine1_GotFocus()
  757.     txtAddressLine1.SelStart = 0
  758.     txtAddressLine1.SelLength = Len(txtAddressLine1)
  759. End Sub
  760. Private Sub txtCity_GotFocus()
  761.     txtCity.SelStart = 0
  762.     txtCity.SelLength = Len(txtCity)
  763. End Sub
  764. Private Sub txtCity_KeyPress(KeyAscii As Integer)
  765.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  766.     txtCity = Trim$(txtCity)
  767.     txtState.SetFocus
  768. End Sub
  769. Private Sub txtCompany_GotFocus()
  770.     txtCompany.SelStart = 0
  771.     txtCompany.SelLength = Len(txtCompany)
  772. End Sub
  773. Private Sub txtCompany_KeyPress(KeyAscii As Integer)
  774.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  775.     txtCompany = Trim$(txtCompany)
  776.     txtAddressLine1.SetFocus
  777. End Sub
  778. Private Sub txtState_GotFocus()
  779.     txtState.SelStart = 0
  780.     txtState.SelLength = Len(txtState)
  781. End Sub
  782. Private Sub txtState_KeyPress(KeyAscii As Integer)
  783.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  784.     txtState = Trim$(txtState)
  785.     txtZipCode.SetFocus
  786. End Sub
  787. Private Sub txtZipCode_GotFocus()
  788.     txtZipCode.SelStart = 0
  789.     txtZipCode.SelLength = Len(txtZipCode)
  790. End Sub
  791. Private Sub txtZipCode_KeyPress(KeyAscii As Integer)
  792.     If KeyAscii <> Asc(vbCr) Then Exit Sub
  793.     txtZipCode = Trim$(txtZipCode)
  794.     cmdButton(SaveButton).SetFocus
  795. End Sub
  796.