Imdabaum
07-27-2006, 11:18 AM
Okay here is a fun one. Just got a request to fix a guys Access Application. He has several text fields that are unbound, because each field has information from the same field just related to a input into the AssignedBldg1 text field. I can update the rest of the fields piece by piece, by doing this:
Private Sub AssignedBldg1_AfterUpdate()
On Error Resume Next
Dim test As String
If Nz(AssignedBldg1) = "" Then
GoTo fieldcleared
Else
test = DLookup("[BuildingID]", "Building", "[BuildingID] = AssignedBldg1")
If AssignedBldg1 = test Then
Me.AsBldgName1 = DLookup("[Title]", "Building", "[BuildingID] = AssignedBldg1")
Me.O6unit1 = DLookup("[06 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.O7units1 = DLookup("[07 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.O8units1 = DLookup("[08 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.BldgCap1 = DLookup("[BuildingCapacity]", "Building", "[BuildingID] = AssignedBldg1")
Me.bldgutil1 = DLookup("[NumWardsAssigned]", "Building", "[BuildingID] = AssignedBldg1")
Else
Me.AsBldgName1 = "" 'These are all textboxes.
Me.O6vacant1 = 0
Me.O6unit1 = 0
Me.O7vacant1 = 0
Me.O7units1 = 0
Me.O8vacant1 = 0
Me.O8units1 = 0
Me.BldgCap1 = 0
Me.bldgutil1 = 0
GoTo Err_PROPERTY_NBR
End If
End If
Exit_AssignedBldg1_AfterUpdate:
Exit Sub
fieldcleared:
Me.AsBldgName1 = "" 'Text fields are cleared if the box is deleted
Me.O6vacant1 = 0
Me.O6unit1 = 0
Me.O7vacant1 = 0
Me.O7units1 = 0
Me.O8vacant1 = 0
Me.O8units1 = 0
Me.BldgCap1 = 0
Me.bldgutil1 = 0
Exit Sub
Err_PROPERTY_NBR:
MsgBox AssignedBldg1 & " was not found.", vbCritical, "Error: Property Number is invalid"
AssignedBldg1 = ""
Me.AssignedBldg1.SetFocus
End Sub
The problem is... he wants 9 more text fields that do the same thing all fields storing information from the same table field just related to different IDs. Is there an easier way to do this than repeating this code 10 times? Can you bind text fields to table fields defining criteria?
Private Sub AssignedBldg1_AfterUpdate()
On Error Resume Next
Dim test As String
If Nz(AssignedBldg1) = "" Then
GoTo fieldcleared
Else
test = DLookup("[BuildingID]", "Building", "[BuildingID] = AssignedBldg1")
If AssignedBldg1 = test Then
Me.AsBldgName1 = DLookup("[Title]", "Building", "[BuildingID] = AssignedBldg1")
Me.O6unit1 = DLookup("[06 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.O7units1 = DLookup("[07 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.O8units1 = DLookup("[08 Units Reserved]", "Building", "[BuildingID] = AssignedBldg1")
Me.BldgCap1 = DLookup("[BuildingCapacity]", "Building", "[BuildingID] = AssignedBldg1")
Me.bldgutil1 = DLookup("[NumWardsAssigned]", "Building", "[BuildingID] = AssignedBldg1")
Else
Me.AsBldgName1 = "" 'These are all textboxes.
Me.O6vacant1 = 0
Me.O6unit1 = 0
Me.O7vacant1 = 0
Me.O7units1 = 0
Me.O8vacant1 = 0
Me.O8units1 = 0
Me.BldgCap1 = 0
Me.bldgutil1 = 0
GoTo Err_PROPERTY_NBR
End If
End If
Exit_AssignedBldg1_AfterUpdate:
Exit Sub
fieldcleared:
Me.AsBldgName1 = "" 'Text fields are cleared if the box is deleted
Me.O6vacant1 = 0
Me.O6unit1 = 0
Me.O7vacant1 = 0
Me.O7units1 = 0
Me.O8vacant1 = 0
Me.O8units1 = 0
Me.BldgCap1 = 0
Me.bldgutil1 = 0
Exit Sub
Err_PROPERTY_NBR:
MsgBox AssignedBldg1 & " was not found.", vbCritical, "Error: Property Number is invalid"
AssignedBldg1 = ""
Me.AssignedBldg1.SetFocus
End Sub
The problem is... he wants 9 more text fields that do the same thing all fields storing information from the same table field just related to different IDs. Is there an easier way to do this than repeating this code 10 times? Can you bind text fields to table fields defining criteria?