home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap35 / frmlooku.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-09  |  15.6 KB  |  430 lines

  1. VERSION 4.00
  2. Begin VB.Form frmLookup 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Lookup Customer"
  6.    ClientHeight    =   4605
  7.    ClientLeft      =   2865
  8.    ClientTop       =   1110
  9.    ClientWidth     =   6255
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   5010
  21.    Left            =   2805
  22.    LinkTopic       =   "Form1"
  23.    MaxButton       =   0   'False
  24.    ScaleHeight     =   4605
  25.    ScaleWidth      =   6255
  26.    Top             =   765
  27.    Width           =   6375
  28.    Begin VB.ComboBox lstSearch 
  29.       Height          =   300
  30.       ItemData        =   "FRMLOOKU.frx":0000
  31.       Left            =   2040
  32.       List            =   "FRMLOOKU.frx":0002
  33.       Style           =   2  'Dropdown List
  34.       TabIndex        =   1
  35.       Top             =   2400
  36.       Width           =   2775
  37.    End
  38.    Begin VB.ComboBox lstmultimatch 
  39.       Height          =   300
  40.       ItemData        =   "FRMLOOKU.frx":0004
  41.       Left            =   600
  42.       List            =   "FRMLOOKU.frx":0006
  43.       Sorted          =   -1  'True
  44.       Style           =   2  'Dropdown List
  45.       TabIndex        =   3
  46.       Top             =   1920
  47.       Visible         =   0   'False
  48.       Width           =   5175
  49.    End
  50.    Begin VB.TextBox txtCustID 
  51.       BeginProperty Font 
  52.          name            =   "MS Sans Serif"
  53.          charset         =   1
  54.          weight          =   700
  55.          size            =   12
  56.          underline       =   0   'False
  57.          italic          =   0   'False
  58.          strikethrough   =   0   'False
  59.       EndProperty
  60.       Height          =   420
  61.       Left            =   2400
  62.       TabIndex        =   0
  63.       Top             =   1320
  64.       Width           =   2295
  65.    End
  66.    Begin VB.CommandButton btnOK2 
  67.       Appearance      =   0  'Flat
  68.       BackColor       =   &H80000005&
  69.       Caption         =   "&OK"
  70.       Enabled         =   0   'False
  71.       Height          =   615
  72.       Left            =   2760
  73.       TabIndex        =   4
  74.       Top             =   3360
  75.       Visible         =   0   'False
  76.       Width           =   1095
  77.    End
  78.    Begin VB.CommandButton btnCancel 
  79.       Appearance      =   0  'Flat
  80.       BackColor       =   &H80000005&
  81.       Caption         =   "&Cancel"
  82.       Height          =   615
  83.       Left            =   4440
  84.       TabIndex        =   5
  85.       TabStop         =   0   'False
  86.       Top             =   3360
  87.       Width           =   1095
  88.    End
  89.    Begin VB.CommandButton btnOK1 
  90.       Appearance      =   0  'Flat
  91.       BackColor       =   &H80000005&
  92.       Caption         =   "&OK"
  93.       Height          =   615
  94.       Index           =   0
  95.       Left            =   1080
  96.       TabIndex        =   2
  97.       Top             =   3360
  98.       Width           =   1095
  99.    End
  100.    Begin VB.Label lblin 
  101.       Alignment       =   2  'Center
  102.       Appearance      =   0  'Flat
  103.       BackColor       =   &H00C0C0C0&
  104.       Caption         =   "in"
  105.       BeginProperty Font 
  106.          name            =   "MS Sans Serif"
  107.          charset         =   1
  108.          weight          =   700
  109.          size            =   9.75
  110.          underline       =   0   'False
  111.          italic          =   0   'False
  112.          strikethrough   =   0   'False
  113.       EndProperty
  114.       ForeColor       =   &H80000008&
  115.       Height          =   255
  116.       Left            =   1560
  117.       TabIndex        =   8
  118.       Top             =   2400
  119.       Width           =   375
  120.    End
  121.    Begin VB.Label Label1 
  122.       Appearance      =   0  'Flat
  123.       BackColor       =   &H00C0C0C0&
  124.       Caption         =   $"FRMLOOKU.frx":0008
  125.       ForeColor       =   &H80000008&
  126.       Height          =   495
  127.       Left            =   600
  128.       TabIndex        =   7
  129.       Top             =   360
  130.       Width           =   5055
  131.    End
  132.    Begin VB.Label lblFind 
  133.       Appearance      =   0  'Flat
  134.       BackColor       =   &H00C0C0C0&
  135.       Caption         =   "Find"
  136.       BeginProperty Font 
  137.          name            =   "MS Sans Serif"
  138.          charset         =   1
  139.          weight          =   700
  140.          size            =   9.75
  141.          underline       =   0   'False
  142.          italic          =   0   'False
  143.          strikethrough   =   0   'False
  144.       EndProperty
  145.       ForeColor       =   &H80000008&
  146.       Height          =   255
  147.       Left            =   1560
  148.       TabIndex        =   6
  149.       Top             =   1440
  150.       Width           =   495
  151.    End
  152. Attribute VB_Name = "frmLookup"
  153. Attribute VB_Creatable = False
  154. Attribute VB_Exposed = False
  155. Private Sub cmbSrchField_KeyPress(KeyAscii As Integer)
  156.     If KeyAscii = 13 Then
  157.         SendKeys "{tab}"
  158.         KeyAscii = 0
  159.     End If
  160. End Sub
  161. Private Sub btnCancel_Click()
  162.     ' User changed their mind.  Reset the Dynaset to
  163.     ' the old customer.
  164.     Call FindCust(ByVal tmpCustNum, "CustomerNum")
  165.     Unload frmLookup
  166. End Sub
  167. Private Sub btnOK1_Click(Index As Integer)
  168. Dim SrchField As String
  169. Dim foundcust As Integer
  170. Dim ListCount As Integer
  171. Dim addcriteria As String
  172. Dim mbox
  173.     'On Error GoTo lookuperr
  174.     MousePointer = 11 ' hourglass
  175.     ' Check if any values have been left out from the search.  If
  176.     ' So, let the user know what they should do.
  177.     If Len(txtCustID.Text) = 0 Then
  178.         MsgBox ("Please enter a value to search for.")
  179.         txtCustID.SetFocus
  180.         MousePointer = 0
  181.         Exit Sub
  182.     End If
  183.     If Len(Trim$(lstSearch.Text)) = 0 Then
  184.         MsgBox ("Please select a field name in which you would like to find '" & txtCustID.Text) & "'"
  185.         lstSearch.SetFocus
  186.         MousePointer = 0
  187.         Exit Sub
  188.     End If
  189.     'Back up the customer number in case the search is unsuccessful.
  190.     tmpCustNum = Customer_number
  191.     ' Assign the database field based on the selection in the list box
  192.     Select Case lstSearch
  193.         Case "Customer Number":
  194.             If IsNumeric(txtCustID.Text) = False Then
  195.                 mbox = MsgBox("Customer number must be numeric.", MB_ICONSTOP)
  196.                 MousePointer = 0
  197.                 txtCustID.Text = ""
  198.                 txtCustID.SetFocus
  199.                 Exit Sub
  200.             End If
  201.             SrchField = "CustomerNum"
  202.         Case "Address": SrchField = "Address"
  203.         Case "Company": SrchField = "Company"
  204.         Case "Last Name": SrchField = "Last_name"
  205.         Case Else:
  206.             Customer_number = tmpCustNum
  207.             MousePointer = 0
  208.             Exit Sub
  209.     End Select
  210.     ' Call the FindCust function.  This function will return
  211.     ' True if it set the customer number to a new number,
  212.     ' False if it was unable to,
  213.     ' or the number of matches if more than one record matched the criteria.
  214.     foundcust = FindCust(ByVal txtCustID, SrchField)
  215.     ' Found none
  216.     If foundcust = False Then
  217.         MsgBox ("Can not find " & lstSearch & " '" & txtCustID & "' in the database.")
  218.         txtCustID.Text = ""
  219.         Customer_number = tmpCustNum
  220.         MousePointer = 0
  221.         Exit Sub
  222.     Else
  223.         ' Found one
  224.         If foundcust = True Then
  225.             'If we found who we wanted, we are done with this screen
  226.             Unload frmLookup
  227.             Exit Sub
  228.         Else
  229.             ' Found more than one.
  230.             ' Get the correct number of records by going to the last one
  231.             CustmainDynaset.MoveLast
  232.             'Tell the user how many were found.
  233.             Label1.Caption = "Found " & CustmainDynaset.RecordCount & " '" & UCase(txtCustID.Text) & "'s.  Please select the one you want."
  234.             ' Turn off the parts of the screen that don't apply now.
  235.             lblFind.Visible = False
  236.             lblin.Visible = False
  237.             txtCustID.Visible = False
  238.             lstSearch.Visible = False
  239.             btnOK1(0).Visible = False
  240.             lblFind.Enabled = False
  241.             lblin.Enabled = False
  242.             txtCustID.Enabled = False
  243.             lstSearch.Enabled = False
  244.             btnOK1(0).Enabled = False
  245.             ' Activate & place the lstMultiMatch control and
  246.             ' associated controls.
  247.             lstmultimatch.Visible = True
  248.             lstmultimatch.Enabled = True
  249.             btnOK2.Visible = True
  250.             btnOK2.Enabled = True
  251.             btnOK2.Left = 1080
  252.             btnOK2.Top = 3360
  253.             'Make sure the list empty before adding items to it.
  254.             lstmultimatch.Clear
  255.             ' Go to the beginning of the dynaset and populate the list.
  256.             CustmainDynaset.MoveFirst
  257.             ' Put a default value in the control
  258.             addcriteria = Str$(CustmainDynaset.Fields("CustomerNum"))
  259.             If IsNull(CustmainDynaset.Fields("First_Name")) <> True Then
  260.                 addcriteria = addcriteria & ", " & CustmainDynaset.Fields("First_Name")
  261.             End If
  262.             If IsNull(CustmainDynaset.Fields("Last_name")) <> True Then
  263.                 addcriteria = addcriteria & " " & CustmainDynaset.Fields("Last_Name")
  264.             End If
  265.             If IsNull(CustmainDynaset.Fields("Company")) <> True Then
  266.                 addcriteria = addcriteria & ", " & CustmainDynaset.Fields("Company")
  267.             End If
  268.             If IsNull(CustmainDynaset.Fields("Address")) <> True Then
  269.                 addcriteria = addcriteria & ", " & CustmainDynaset.Fields("Address")
  270.             End If
  271.             ' start populating the combo box list
  272.             ListCount = 0
  273.             While CustmainDynaset.RecordCount > ListCount
  274.                 addcriteria = Str$(CustmainDynaset.Fields("CustomerNum"))
  275.                 If IsNull(CustmainDynaset.Fields("First_Name")) <> True Then
  276.                     addcriteria = addcriteria & ", " & CustmainDynaset.Fields("First_Name")
  277.                 End If
  278.                 If IsNull(CustmainDynaset.Fields("Last_name")) <> True Then
  279.                     addcriteria = addcriteria & " " & CustmainDynaset.Fields("Last_Name")
  280.                 End If
  281.                 If IsNull(CustmainDynaset.Fields("Company")) <> True Then
  282.                     addcriteria = addcriteria & ", " & CustmainDynaset.Fields("Company")
  283.                 End If
  284.                 If IsNull(CustmainDynaset.Fields("Address")) <> True Then
  285.                     addcriteria = addcriteria & ", " & CustmainDynaset.Fields("Address")
  286.                 End If
  287.                 lstmultimatch.AddItem addcriteria
  288.                 CustmainDynaset.MoveNext
  289.                 ListCount = ListCount & 1
  290.             Wend
  291.         End If
  292.     End If
  293.     MousePointer = 0 ' hourglass
  294.     Exit Sub
  295. lookuperr:
  296.     MsgBox Err.Description
  297. End Sub
  298. Private Sub btnOK2_Click()
  299. Dim foundcust As Integer
  300. Dim tmpCustNum
  301. Dim srchCustNumstr As String
  302. Dim srchCustNum
  303. Dim custnumlen As Integer
  304.     MousePointer = 11 ' hourglass
  305.     tmpCustNum = Customer_number
  306.     If Len(lstmultimatch.Text) < 1 Then
  307.         MsgBox ("Please select an item from the list or press Cancel.")
  308.         MousePointer = 0
  309.         Exit Sub
  310.     End If
  311.     ' The length of the customer number is everything up
  312.     ' until the comma, so find out where the comma is,
  313.     ' and subtract 1 to get the length of the customer num.
  314.     custnumlen = (InStr(lstmultimatch.Text, ", "))
  315.     custnumlen = custnumlen - 1
  316.     ' Get the customer number from the listbox text
  317.     srchCustNumstr = Mid$(lstmultimatch.Text, 1, custnumlen)
  318.     srchCustNum = Val(srchCustNumstr)
  319.     ' Use the customer number to create the dynaset.
  320.     foundcust = FindCust(ByVal srchCustNum, "CustomerNum")
  321.     If foundcust = True Then
  322.         MousePointer = 0
  323.         Unload frmLookup
  324.         Exit Sub
  325.     Else
  326.         MsgBox ("Error in search, please retry with a different lookup field.")
  327.         Customer_number = tmpCustNum
  328.     End If
  329.         MousePointer = 0 ' default
  330. End Sub
  331. Private Sub CSCombo1_Click()
  332.     If KeyAscii = 13 Then
  333.         SendKeys "{tab}"
  334.         KeyAscii = 0
  335.     End If
  336. End Sub
  337. Private Function FindCust(ByVal CustID As String, SrchField) As Integer
  338. Dim tmpCustNum
  339. ' CustID is the actual text to use for the search
  340. ' SrchField is the name of the field to use, which could be
  341. ' any field in the CUSTMAIN table.
  342.     tmpCustNum = Customer_number
  343.     'If there is no active record,
  344.     'then return without doing anything
  345.     If Len(CustID) = 0 Or Len(SrchField) = 0 Then
  346.         FindCust = False
  347.         Exit Function
  348.     Else
  349.         ' Set one criteria if the user is searching by Number
  350.         If SrchField = "CustomerNum" Then
  351.             Qcriteria = "SELECT * FROM CUSTMAIN WHERE " & SrchField & " = " & CustID & ";"
  352.         Else
  353.         ' Set a different criteria for an non-numeric searches
  354.             Qcriteria = "SELECT * FROM CUSTMAIN WHERE " & SrchField & " LIKE """ & CustID & """;"
  355.         End If
  356.         ' Create the dynaset
  357.         Set CustmainDynaset = CustDB.CreateDynaset(Qcriteria)
  358.         ' Check for a successful record search.
  359.         ' Populate the fields on the screen if a record is found.
  360.         If CustmainDynaset.RecordCount = 0 Then
  361.             ' Could not find the customer,
  362.             ' so reset the Dynaset back to the original customer.
  363.             ' If there was no original customer, then just get out.
  364.             If Len(tmpCustNum) = 0 Then
  365.                 FindCust = False
  366.                 Exit Function
  367.             End If
  368.             Customer_number = tmpCustNum
  369.             Qcriteria = "SELECT * FROM CUSTMAIN WHERE CustomerNum" & " = " & Customer_number & ";"
  370.             Set CustmainDynaset = CustDB.CreateDynaset(Qcriteria)
  371.             FindCust = False
  372.         Else
  373.             ' Get to the last record so we can determine how
  374.             ' many matches there were.
  375.             CustmainDynaset.MoveLast
  376.             
  377.             ' Found more than 1 match
  378.             If CustmainDynaset.RecordCount > 1 Then
  379.                 Customer_number = tmpCustNum
  380.                 FindCust = CustmainDynaset.RecordCount
  381.             Else
  382.                 ' Found only 1 match
  383.                 Customer_number = CustmainDynaset.Fields("CustomerNum")
  384.                 FindCust = True
  385.             End If
  386.         End If
  387.     End If
  388. End Function
  389. Private Sub Form_Activate()
  390.     ' Reset to the default  pointer when returning to this form.
  391.     MousePointer = 0
  392.     lstSearch = "Customer Number"
  393. End Sub
  394. Private Sub Form_Load()
  395.     Left = (Screen.Width - Width) / 2
  396.     Top = (Screen.Height - Height) / 2
  397.     lstSearch.AddItem "Customer Number"
  398.     lstSearch.AddItem "Address"
  399.     lstSearch.AddItem "Company"
  400.     lstSearch.AddItem "Last Name"
  401. End Sub
  402. Private Sub lstmultimatch_KeyDown(KeyCode As Integer, Shift As Integer)
  403.     If KeyCode = KEY_RETURN Then
  404.         btnOK2.SetFocus
  405.     End If
  406. End Sub
  407. Private Sub lstSearch_KeyDown(KeyCode As Integer, Shift As Integer)
  408.     If KeyCode = KEY_RETURN Then
  409.         btnOK1(0).SetFocus
  410.     End If
  411. End Sub
  412. Private Sub lstSearch_KeyPress(KeyAscii As Integer)
  413.     If KeyAscii = 13 Then
  414.         SendKeys "{tab}"
  415.         KeyAscii = 0
  416.     End If
  417. End Sub
  418. Private Sub lstSrchField_KeyPress(KeyAscii As Integer)
  419.     If KeyAscii = 13 Then
  420.         SendKeys "{tab}"
  421.         KeyAscii = 0
  422.     End If
  423. End Sub
  424. Private Sub txtCustID_KeyPress(KeyAscii As Integer)
  425.     If KeyAscii = 13 Then
  426.         SendKeys "{tab}"
  427.         KeyAscii = 0
  428.     End If
  429. End Sub
  430.