Advertisement
jdelano

MS Access

Apr 6th, 2025 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 18.90 KB | None | 0 0
  1. Option Compare Database
  2.  
  3. Private Type familyType
  4.     familyID As Integer
  5.     lastName As String
  6.     firstName As String
  7.     address1 As String
  8.     address2 As String
  9.     city As String
  10.     state As String
  11.     zipCode As String
  12. End Type
  13.  
  14. Dim familyArray() As familyType
  15. Dim currentArrayIndex As Integer
  16. Dim selectedPlate As String
  17.  
  18. Private Sub btnNewFamily_Click()
  19.    
  20.     ' indicate a new family is being added, set teh user at the lastname to begin and enable the save button
  21.    lblFamilyTitle.Caption = "New Family"
  22.     txtLastName.SetFocus
  23.    
  24.     currentArrayIndex = 0 ' this makes the next like clear the form
  25.    
  26.     DisplayAssignedFamily
  27.    
  28.     btnNewFamily.Enabled = False
  29.     btnSaveFamily.Enabled = True
  30.    
  31.     SetPreNextButtonState
  32.    
  33. End Sub
  34.  
  35. Private Sub btnNextFamily_Click()
  36.  
  37.     ' display the next family assigned to the plate
  38.    currentArrayIndex = currentArrayIndex + 1
  39.     DisplayAssignedFamily
  40.    
  41.     SetPreNextButtonState
  42. End Sub
  43.  
  44. Private Sub btnPreviousFamily_Click()
  45.  
  46.     ' display the previous family assigned to the plate
  47.    currentArrayIndex = currentArrayIndex - 1
  48.     DisplayAssignedFamily
  49.    
  50.     SetPreNextButtonState
  51.    
  52. End Sub
  53.  
  54. Private Sub btnSaveFamily_Click()
  55.  
  56.     If lblFamilyTitle.Caption = "New Family" Then
  57.         SaveNewFamilyData
  58.     Else
  59.         SaveEditedFamilyData
  60.     End If
  61.    
  62. End Sub
  63.  
  64. Private Sub SaveNewFamilyData()
  65.  
  66.     Dim validateResult As String
  67.     validateResult = ValidateFamilyData
  68.    
  69.     If Len(validateResult) > 0 Then
  70.         MsgBox validateResult, vbExclamation, "Failed Validation"
  71.         Exit Sub
  72.     End If
  73.  
  74.     ' save the new family
  75.    Dim insertSQL As String
  76.     insertSQL = "Insert into tblFamilies (StatePlate, LastName, FirstName, Address1, Address2, City, State, ZipCode) " & _
  77.         "Values ('" & selectedPlate & "',"
  78.    
  79.     If UBound(familyArray) = 0 Then
  80.         ' there are no family records for this plate
  81.        currentArrayIndex = 1
  82.        
  83.     Else
  84.         currentArrayIndex = currentArrayIndex + 1
  85.     End If
  86.    
  87.     ReDim Preserve familyArray(UBound(familyArray) + 1)
  88.    
  89.     ' add each value to the insert query
  90.    Dim c As Control
  91.     For Each c In Me.Controls
  92.         If TypeOf c Is TextBox Then
  93.             If c.Tag = "FamilyTextBox" Then
  94.                 c.SetFocus
  95.                 Select Case c.Name
  96.                     Case "txtLastName"
  97.                         insertSQL = insertSQL & "'" & Replace(c.Text, "'", "''") & "',"
  98.                         familyArray(currentArrayIndex).lastName = c.Text
  99.                        
  100.                     Case "txtFirstName"
  101.                         insertSQL = insertSQL & "'" & Replace(c.Text, "'", "''") & "',"
  102.                         familyArray(currentArrayIndex).firstName = c.Text
  103.                        
  104.                     Case "txtAddress1"
  105.                         insertSQL = insertSQL & "'" & Replace(c.Text, "'", "''") & "',"
  106.                         familyArray(currentArrayIndex).address1 = c.Text
  107.                        
  108.                     Case "txtAddress2"
  109.                         insertSQL = insertSQL & "'" & Replace(c.Text, "'", "''") & "',"
  110.                         familyArray(currentArrayIndex).address2 = c.Text
  111.                        
  112.                     Case "txtCity"
  113.                         insertSQL = insertSQL & "'" & Replace(c.Text, "'", "''") & "',"
  114.                         familyArray(currentArrayIndex).city = c.Text
  115.                        
  116.                     Case "txtState"
  117.                         insertSQL = insertSQL & "'" & c.Text & "',"
  118.                         familyArray(currentArrayIndex).state = c.Text
  119.                        
  120.                     Case "txtZipCode"
  121.                         insertSQL = insertSQL & "'" & c.Text & "')"
  122.                         familyArray(currentArrayIndex).zipCode = c.Text
  123.                
  124.                 End Select
  125.                
  126.             End If
  127.         End If
  128.     Next c
  129.    
  130.     CurrentDb.Execute insertSQL
  131.    
  132.     ' update the array with the id assigned to the family inserted into the table
  133.    Dim rs As Recordset
  134.     Set rs = CurrentDb.OpenRecordset("Select max(id) from tblFamilies where StatePlate='" & selectedPlate & "'", dbOpenSnapshot)
  135.     familyArray(currentArrayIndex).familyID = rs(0)
  136.     rs.Close
  137.    
  138.     btnSaveFamily.Enabled = False ' save is done
  139.    btnNewFamily.Enabled = True   ' a new family can be added now
  140.    
  141.     lblFamilyTitle.Caption = "Family " & CStr(currentArrayIndex) & " of " & CStr(UBound(familyArray) - 1)
  142.    
  143.     ' fill some data for today's visit
  144.    lblSelectedFamily.Caption = familyArray(currentArrayIndex).lastName & ", " & familyArray(currentArrayIndex).firstName
  145.     lblVisitDateTime.Caption = Format(Now, "MM/dd/yy hh:mm:ss AM/PM")
  146.    
  147.     ' put the user in the first count field for today's visit
  148.    Me.txtCountHispanic.SetFocus
  149.    
  150.     ' re-enable prev and next if needed
  151.    SetPreNextButtonState
  152.    
  153. End Sub
  154.  
  155. Private Sub SaveEditedFamilyData()
  156.  
  157.     Dim validateResult As String
  158.     validateResult = ValidateFamilyData
  159.    
  160.     If Len(validateResult) > 0 Then
  161.         MsgBox validateResult, vbExclamation, "Failed Validation"
  162.         Exit Sub
  163.     End If
  164.  
  165.     ' save the pending edits
  166.    Dim fieldName As String
  167.    
  168.     Dim c As Control
  169.     For Each c In Me.Controls
  170.         If TypeOf c Is TextBox Then
  171.             If c.Tag = "FamilyTextBox" Then
  172.                 ' if even 1 textbox is marked yellow than an edit hasn't been saved
  173.                If c.BackColor = vbYellow Then
  174.                     Select Case c.Name
  175.                         Case "txtLastName"
  176.                             fieldName = "lastName"
  177.                         Case "txtFirstName"
  178.                             fieldName = "firstName"
  179.                         Case "txtAddress1"
  180.                             fieldName = "address1"
  181.                         Case "txtAddress2"
  182.                             fieldName = "address2"
  183.                         Case "txtCity"
  184.                             fieldName = "city"
  185.                         Case "txtState"
  186.                             fieldName = "state"
  187.                         Case "txtZipCode"
  188.                             fieldName = "zipCode"
  189.                    
  190.                     End Select
  191.                
  192.                     SaveEditedValue c, fieldName
  193.                     c.BackColor = vbWhite
  194.                 End If
  195.             End If
  196.         End If
  197.     Next c
  198.    
  199.     btnSaveFamily.Enabled = False ' save is done
  200.    btnNewFamily.Enabled = True   ' a new family can be added now
  201.    
  202.     ' re-enable prev and next if needed
  203.    SetPreNextButtonState
  204. End Sub
  205.  
  206.  
  207. Private Sub SetPreNextButtonState()
  208.  
  209.     ' set the state of teh new and prev buttons
  210.    btnPreviousFamily.Enabled = currentArrayIndex > 1 And lblFamilyTitle.Caption <> "New Family"
  211.     btnNextFamily.Enabled = currentArrayIndex < UBound(familyArray) - 1 And lblFamilyTitle.Caption <> "New Family"
  212.  
  213. End Sub
  214.  
  215. Private Sub SaveEditedValue(txtBox As TextBox, fieldName As String)
  216.  
  217.     ' write the edit to the database - use chr(34) so that if there is an ' in the field it won't cause a query error
  218.    txtBox.SetFocus
  219.     CurrentDb.Execute "Update tblFamilies Set " & fieldName & "=" & Chr(34) & txtBox.Text & Chr(34) & " where id=" & familyArray(currentArrayIndex).familyID
  220.  
  221.     ' save the new value to the array as well
  222.    Select Case txtBox.Name
  223.         Case "txtLastName"
  224.             familyArray(currentArrayIndex).lastName = txtBox.Text
  225.         Case "txtFirstName"
  226.             familyArray(currentArrayIndex).firstName = txtBox.Text
  227.         Case "txtAddress1"
  228.             familyArray(currentArrayIndex).address1 = txtBox.Text
  229.         Case "txtAddress2"
  230.             familyArray(currentArrayIndex).address2 = txtBox.Text
  231.         Case "txtCity"
  232.             familyArray(currentArrayIndex).city = txtBox.Text
  233.         Case "txtState"
  234.             familyArray(currentArrayIndex).state = txtBox.Text
  235.         Case "txtZipCode"
  236.             familyArray(currentArrayIndex).zipCode = txtBox.Text
  237.    
  238.     End Select
  239.    
  240.     ' check to see if the first or last name was edited, if so update the value in today's visit section
  241.    If InStr(txtBox.Name, "name") > 0 Then lblSelectedFamily.Caption = familyArray(currentArrayIndex).lastName & ", " & familyArray(currentArrayIndex).firstName
  242.    
  243. End Sub
  244.  
  245.  
  246. Private Sub btnSavePlate_Click()
  247.  
  248.     ' the plate was not found in the plates table, the user has selected to save the plate to the database
  249.    txtPlate.SetFocus
  250.     selectedPlate = txtPlate.Text
  251.    
  252.     If MsgBox("Are you sure you want to save the plate '" & selectedPlate & "'?", vbYesNo, "Confirm New Plate") = vbNo Then Exit Sub
  253.        
  254.     ' insert the plate number in the database
  255.    CurrentDb.Execute "Insert Into tblPlates Values ('" & selectedPlate & "');"
  256.    
  257.     btnSavePlate.Enabled = False ' no longer needs to be enabled as the plate has been saved
  258.  
  259.     btnNewFamily_Click  ' set the user in the add new family mode
  260.    
  261. End Sub
  262.  
  263. Private Sub Form_Activate()
  264.    
  265.     txtPlate.SetFocus
  266.     txtPlate.Text = ""
  267.    
  268.     lbMatchingPlates.Selected(0) = False
  269.        
  270. End Sub
  271.  
  272. Private Sub lbMatchingPlates_Click()
  273.  
  274.     ' exit the click event if there is nothing in the listbox
  275.    If IsNull(lbMatchingPlates.ItemData(0)) Then Exit Sub
  276.  
  277.     ' reset the move buttons
  278.    btnNextFamily.Enabled = False
  279.     btnPreviousFamily.Enabled = False
  280.  
  281.     ' find the families assigned to the selected plate
  282.    selectedPlate = lbMatchingPlates.ItemData(lbMatchingPlates.ListIndex)
  283.    
  284.     Dim familyRS As Recordset
  285.     Set familyRS = CurrentDb.OpenRecordset("Select * from tblFamilies Where StatePlate='" & selectedPlate & "' Order by ID", dbOpenSnapshot)
  286.    
  287.     ReDim familyArray(familyRS.RecordCount + 1)
  288.    
  289.     ' if more than 1 family then display the count and enable the next record button
  290.    If familyRS.RecordCount > 1 Then
  291.         lblFamilyTitle.Caption = "Family 1 of " & CStr(familyRS.RecordCount)
  292.         btnNextFamily.Enabled = True
  293.        
  294.         ' fill the array that will be used by the next and prev buttons
  295.        Dim familyCount As Integer
  296.        
  297.         familyCount = 1
  298.         Do While Not familyRS.EOF
  299.             familyArray(familyCount).familyID = familyRS("ID")
  300.             familyArray(familyCount).lastName = familyRS("lastname")
  301.             familyArray(familyCount).firstName = familyRS("firstname")
  302.             familyArray(familyCount).address1 = familyRS("Address1")
  303.             familyArray(familyCount).address2 = familyRS("Address2") & ""
  304.             familyArray(familyCount).city = familyRS("city")
  305.             familyArray(familyCount).state = familyRS("state")
  306.             familyArray(familyCount).zipCode = familyRS("zipcode")
  307.             familyRS.MoveNext
  308.            
  309.             familyCount = familyCount + 1
  310.         Loop
  311.        
  312.     Else
  313.         lblFamilyTitle.Caption = "Family"
  314.        
  315.         familyArray(1).familyID = familyRS("ID")
  316.         familyArray(1).lastName = familyRS("lastname")
  317.         familyArray(1).firstName = familyRS("firstname")
  318.         familyArray(1).address1 = familyRS("Address1")
  319.         familyArray(1).address2 = familyRS("Address2") & ""
  320.         familyArray(1).city = familyRS("city")
  321.         familyArray(1).state = familyRS("state")
  322.         familyArray(1).zipCode = familyRS("zipcode")
  323.        
  324.     End If
  325.    
  326.     familyRS.Close
  327.     Set familyRS = Nothing
  328.    
  329.     currentArrayIndex = 1
  330.    
  331.     btnNewFamily.Enabled = True
  332.    
  333.     ' display the family record
  334.    DisplayAssignedFamily
  335.    
  336. End Sub
  337.  
  338. Private Sub DisplayAssignedFamily()
  339.  
  340.     ' fill the controls with family data
  341.    txtLastName.Value = familyArray(currentArrayIndex).lastName
  342.     txtFirstName.Value = familyArray(currentArrayIndex).firstName
  343.     txtAddress1.Value = familyArray(currentArrayIndex).address1
  344.     txtAddress2.Value = familyArray(currentArrayIndex).address2
  345.     txtCity.Value = familyArray(currentArrayIndex).city
  346.     txtState.Value = familyArray(currentArrayIndex).state
  347.     txtZipCode.Value = familyArray(currentArrayIndex).zipCode
  348.  
  349.     If currentArrayIndex > 0 Then
  350.         ' reuse the section of the family title that has the number of families assigned to the plate
  351.        ' but increment the number that is being viewed
  352.        lblFamilyTitle.Caption = "Family " & CStr(currentArrayIndex) & Mid(lblFamilyTitle.Caption, InStr(lblFamilyTitle.Caption, " of"))
  353.        
  354.         ' fill some data for today's visit
  355.        lblSelectedFamily.Caption = familyArray(currentArrayIndex).lastName & ", " & familyArray(currentArrayIndex).firstName
  356.         lblVisitDateTime.Caption = Format(Now, "MM/dd/yy hh:mm:ss AM/PM")
  357.        
  358.         ' put the user in the first count field for today's visit
  359.        Me.txtCountHispanic.SetFocus
  360.     Else
  361.         ' if the index = 0 then the family data section was reset
  362.        lblSelectedFamily.Caption = ""
  363.         lblVisitDateTime.Caption = ""
  364.     End If
  365.    
  366.     SetPreNextButtonState
  367.    
  368. End Sub
  369.  
  370. Private Function ValidateFamilyData() As String
  371.  
  372.     Dim dataValidationMsg As String
  373.     Dim minLength As Integer
  374.     Dim maxLength As Integer
  375.    
  376.     dataValidationMsg = ""
  377.    
  378.     Dim c As Control
  379.     For Each c In Me.Controls
  380.         If TypeOf c Is TextBox Then
  381.             If c.Tag = "FamilyTextBox" Then
  382.                 ' set the expected min and max length of fields to test for
  383.                c.SetFocus
  384.                 Select Case c.Name
  385.                     Case "txtLastName"
  386.                         minLength = 2
  387.                         maxLength = 50
  388.                        
  389.                     Case "txtFirstName"
  390.                         minLength = 2
  391.                         maxLength = 50
  392.                        
  393.                     Case "txtAddress1"
  394.                         minLength = 10
  395.                         maxLength = 150
  396.  
  397.                     Case "txtAddress2"
  398.                         minLength = 0
  399.                         maxLength = 150
  400.  
  401.                     Case "txtCity"
  402.                         minLength = 3
  403.                         maxLength = 100
  404.  
  405.                     Case "txtState"
  406.                         minLength = 2
  407.                         maxLength = 2
  408.  
  409.                     Case "txtZipCode"
  410.                         minLength = 5
  411.                         maxLength = 5
  412.                
  413.                         If Val(c.Text) = 0 Then
  414.                             dataValidationMsg = "The Zip Code must be numeric" & vbCrLf
  415.                         End If
  416.                 End Select
  417.            
  418.                 If Len(c.Text) < minLength Or Len(c.Text) > maxLength Then
  419.                     dataValidationMsg = dataValidationMsg & "The " & Replace(c.Name, "txt", "") & " value must be 2 characters" & vbCrLf
  420.                 End If
  421.            
  422.             End If
  423.            
  424.         End If
  425.     Next c
  426.    
  427.     ' return any message minus the last vbCrLf
  428.    If Len(ValidateFamilyData) > 0 Then ValidateFamilyData = Left(dataValidationMsg, Len(dataValidationMsg) - 1) Else ValidateFamilyData = ""
  429.    
  430.    
  431. End Function
  432.  
  433. Private Sub handleFamilyTextChange(txtBox As TextBox)
  434.  
  435.     ' should the save button be available - because of this one change?
  436.    ' all fields require values except for the address2 field
  437.    ' or if the user is adding a new family to a plate there is no need to indicate an edit
  438.    If (txtBox.Text = "" And txtBox.Name <> "txtAddress2") Or lblFamilyTitle.Caption = "New Family" Then Exit Sub
  439.    
  440.     Dim stateOfSaveButton As Boolean
  441.     Select Case txtBox.Name
  442.         Case "txtLastName"
  443.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).lastName
  444.            
  445.         Case "txtFirstName"
  446.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).firstName
  447.            
  448.         Case "txtAddress1"
  449.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).address1
  450.            
  451.         Case "txtAddress2"
  452.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).address2
  453.            
  454.         Case "txtCity"
  455.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).city
  456.            
  457.         Case "txtState"
  458.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).state
  459.            
  460.         Case "txtZipCode"
  461.             stateOfSaveButton = txtBox.Text <> familyArray(currentArrayIndex).zipCode
  462.    
  463.     End Select
  464.    
  465.     ' show or remove that the field has changed and needs to be saved
  466.    If stateOfSaveButton Then txtBox.BackColor = vbYellow Else txtBox.BackColor = vbWhite
  467.    
  468.     btnSaveFamily.Enabled = stateOfSaveButton
  469.        
  470.     If txtBox.BackColor = vbWhite Then
  471.         ' if the box was set to white then check the other family data fields for changes needing to be saved
  472.        Dim c As Control
  473.         For Each c In Me.Controls
  474.             If TypeOf c Is TextBox Then
  475.                 If c.Tag = "FamilyTextBox" Then
  476.                     ' if even 1 textbox is colored yellow than an edit hasn't been saved
  477.                    If c.BackColor = vbYellow Then btnSaveFamily.Enabled = True
  478.                 End If
  479.             End If
  480.         Next c
  481.     End If
  482.    
  483.     ' the new, previous, or next buttons can't be enabled if there is a save pending
  484.    btnNewFamily.Enabled = Not btnSaveFamily.Enabled
  485.     btnPreviousFamily.Enabled = Not btnSaveFamily.Enabled
  486.     btnNextFamily.Enabled = Not btnSaveFamily.Enabled
  487.    
  488. End Sub
  489.  
  490. Private Sub txtAddress1_Change()
  491.     handleFamilyTextChange txtAddress1
  492. End Sub
  493.  
  494. Private Sub txtAddress2_Change()
  495.     handleFamilyTextChange txtAddress2
  496. End Sub
  497.  
  498. Private Sub txtCity_Change()
  499.     handleFamilyTextChange txtCity
  500. End Sub
  501.  
  502. Private Sub txtFirstName_Change()
  503.     handleFamilyTextChange txtFirstName
  504. End Sub
  505.  
  506. Private Sub txtLastName_Change()
  507.     handleFamilyTextChange txtLastName
  508. End Sub
  509.  
  510. Private Sub txtState_Change()
  511.     handleFamilyTextChange txtState
  512. End Sub
  513.  
  514. Private Sub txtZipCode_Change()
  515.     handleFamilyTextChange txtZipCode
  516. End Sub
  517.  
  518. Private Sub txtPlate_Change()
  519.    
  520.     ' fill the listbox with the matching plates if there is text in the plate text box
  521.    If Len(txtPlate.Text) > 0 Then
  522.         lbMatchingPlates.RowSource = "SELECT tblPlates.StatePlate FROM tblPlates " & _
  523.         "WHERE (((tblPlates.StatePlate) Like " & Chr(34) & "*" & txtPlate.Text & "*" & Chr(34) & ")) ORDER BY tblPlates.StatePlate;"
  524.  
  525.     Else
  526.         lbMatchingPlates.RowSource = "Select top 1 '' as StatePlate from tblPlates"
  527.     End If
  528.    
  529.     ' if there aer no matches, and there is a plate entered in the textbox, enable the "Add Plate" button
  530.    ' to save the plate to the database
  531.    btnSavePlate.Enabled = IIf(IsNull(lbMatchingPlates.ItemData(0)), "", lbMatchingPlates.ItemData(0)) = "" And Len(txtPlate.Text) > 0
  532.    
  533.     'btnNewFamily.Enabled = False
  534.    'btnSaveFamily.Enabled = False
  535.        
  536.     ReDim familyArray(0)
  537.    
  538.     currentArrayIndex = 0
  539.    
  540.     DisplayAssignedFamily
  541.    
  542. End Sub
  543.  
  544.  
  545.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement