F
Frank Situmorang
Heloo,
In one form my ID dereives from chruchID and member sequence number does
work, while the same VBA to combince the addressID with church number does
not work. I do not know what's woring, I do the same in my address form using
Address table. Pls help, below iis the 1stone that workds, while the 2nd one
does not:
Option Compare Database
Option Explicit
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
On Error GoTo Err_Form_BeforeInsert
Set db = DBEngine(0)(0)
' built query string to sort MemberID in descending oder
' where the first part of MemberID matches default church number
strSQL = "SELECT TOP 1 MemberID FROM Members "
strSQL = strSQL & "WHERE (((Left([MemberID], 4)) = """
strSQL = strSQL & Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & """))"
strSQL = strSQL & "ORDER BY MemberID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!MemberID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & "0001"
Else
rst.MoveFirst
' add 1 to MemberID and add leading zeros
Me!MemberID = Right("000" & (rst(0) + 1), 4)
' combine default church number with sequence number
' from previous line
Me!MemberID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & Me.MemberID
End If
DoCmd.GoToControl "FirstName"
rst.Close
Set rst = Nothing
Set db = Nothing
Exit_Form_BeforeInsert:
Exit Sub
Err_Form_BeforeInsert:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_Members Form." & "Form_BeforeInsert")
Resume Exit_Form_BeforeInsert
End Select
End Sub
Private Sub MemberID_Change()
' examine each character as it is entered and if it is not an acceptable
' character then ignore it and leave string as it was before
On Error GoTo Err_MemberID_Change
If InStr(1, "0123456789", Right(Me!MemberID.Text, 1)) = 0 Then
Me!MemberID.Text = Left(Me!MemberID.Text, Len(Me!MemberID.Text) - 1)
End If
' automatically increment the member number. The member number is
' formed by using the church number and adding a sequence number,
' ex., 08930008. Get the church number after user enters 4 numbers,
' find the highest number used for that year, and then add 1.
' If starting anew, add 0001. (This allows a church to have a maxium
' of 9999 members.)
' the user types the first 4 characters, which represents the
' church number
If Len(Me!MemberID.Text) = 4 Then
' if church number typed is different than default church
' number, then tell the user
If Me!MemberID.Text <> Right("000" & DLookup("Church", _
"Defaults"), 4) Then
If vbNo = MsgBox("This is not the default church ID! " _
& "Do you wish to continue", vbQuestion + vbYesNo, "Warning!")
Then
Exit Sub
End If
End If
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
' built query string to sort MemberID in descending oder
' where the first part of MemberID matches characters typed
strSQL = "SELECT TOP 1 MemberID FROM Members "
strSQL = strSQL & "WHERE (((Left([MemberID], 4)) = """
strSQL = strSQL & Me!MemberID.Text & """))"
strSQL = strSQL & "ORDER BY MemberID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!MemberID.Text = Me!MemberID.Text & "0001"
Else
rst.MoveFirst
Me!MemberID.Text = Right("000" & (rst(0) + 1), 8)
End If
DoCmd.GoToControl "FirstName"
rst.Close
Set rst = Nothing
Set db = Nothing
End If
Exit_MemberID_Change:
Exit Sub
Err_MemberID_Change:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_Members Form." & "MemberID_Change")
Resume Exit_MemberID_Change
End Select
End Sub
2ND ONE:
Option Compare Database
Option Explicit
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
On Error GoTo Err_Form_BeforeInsert
Set db = DBEngine(0)(0)
' built query string to sort AddresID in descending oder
' where the first part of AddresID matches default church number
strSQL = "SELECT TOP 1 AddresID FROM KbyAngAlamat"
strSQL = strSQL & "WHERE (((Left([AddresID], 4)) = """
strSQL = strSQL & Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & """))"
strSQL = strSQL & "ORDER BY AddresID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!AddresID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & "0001"
Else
rst.MoveFirst
' add 1 to AddresID and add leading zeros
Me!AddresID = Right("000" & (rst(0) + 1), 4)
' combine default church number with sequence number
' from previous line
Me!AddresID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & Me.AddresID
End If
DoCmd.GoToControl "HOUSEHOLDNAME"
rst.Close
Set rst = Nothing
Set db = Nothing
Exit_Form_BeforeInsert:
Exit Sub
Err_Form_BeforeInsert:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_ALAMAT PER KELUARGA_JEMAAT KBY Form." & "Form_BeforeInsert")
Resume Exit_Form_BeforeInsert
End Select
End Sub
Private Sub AddresID_Change()
' examine each character as it is entered and if it is not an acceptable
' character then ignore it and leave string as it was before
On Error GoTo Err_AddresID_Change
If InStr(1, "0123456789", Right(Me!AddresID.Text, 1)) = 0 Then
Me!AddresID.Text = Left(Me!AddresID.Text, Len(Me!AddresID.Text) - 1)
End If
' automatically increment the AddresID number. The AddresID number is
' formed by using the church number and adding a sequence number,
' ex., 08930008. Get the church number after user enters 4 numbers,
' find the highest number used for that year, and then add 1.
' If starting anew, add 0001. (This allows a church to have a maxium
' of 9999 AddresID.)
' the user types the first 4 characters, which represents the
' church number
If Len(Me!AddresID.Text) = 4 Then
' if church number typed is different than default church
' number, then tell the user
If Me!AddresID.Text <> Right("000" & DLookup("Church", _
"Defaults"), 4) Then
If vbNo = MsgBox("This is not the default church ID! " _
& "Do you wish to continue", vbQuestion + vbYesNo, "Warning!")
Then
Exit Sub
End If
End If
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
' built query string to sort AddresID in descending oder
' where the first part of AddresIDmatches characters typed
strSQL = "SELECT TOP 1 AddresID FROM KbyAngAlamat "
strSQL = strSQL & "WHERE (((Left([AddresID], 4)) = """
strSQL = strSQL & Me!AddresID.Text & """))"
strSQL = strSQL & "ORDER BY AddresID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!AddresID.Text = Me!AddresID.Text & "0001"
Else
rst.MoveFirst
Me!AddresID.Text = Right("000" & (rst(0) + 1), 8)
End If
DoCmd.GoToControl "HOUSEHOLDNAME"
rst.Close
Set rst = Nothing
Set db = Nothing
End If
Exit_AddresID_Change:
Exit Sub
Err_AddresID_Change:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_ALAMAT PER KELUARGA_JEMAAT KBY Form." & "AddresID_Change")
Resume Exit_AddresID_Change
End Select
End Sub
In one form my ID dereives from chruchID and member sequence number does
work, while the same VBA to combince the addressID with church number does
not work. I do not know what's woring, I do the same in my address form using
Address table. Pls help, below iis the 1stone that workds, while the 2nd one
does not:
Option Compare Database
Option Explicit
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
On Error GoTo Err_Form_BeforeInsert
Set db = DBEngine(0)(0)
' built query string to sort MemberID in descending oder
' where the first part of MemberID matches default church number
strSQL = "SELECT TOP 1 MemberID FROM Members "
strSQL = strSQL & "WHERE (((Left([MemberID], 4)) = """
strSQL = strSQL & Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & """))"
strSQL = strSQL & "ORDER BY MemberID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!MemberID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & "0001"
Else
rst.MoveFirst
' add 1 to MemberID and add leading zeros
Me!MemberID = Right("000" & (rst(0) + 1), 4)
' combine default church number with sequence number
' from previous line
Me!MemberID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & Me.MemberID
End If
DoCmd.GoToControl "FirstName"
rst.Close
Set rst = Nothing
Set db = Nothing
Exit_Form_BeforeInsert:
Exit Sub
Err_Form_BeforeInsert:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_Members Form." & "Form_BeforeInsert")
Resume Exit_Form_BeforeInsert
End Select
End Sub
Private Sub MemberID_Change()
' examine each character as it is entered and if it is not an acceptable
' character then ignore it and leave string as it was before
On Error GoTo Err_MemberID_Change
If InStr(1, "0123456789", Right(Me!MemberID.Text, 1)) = 0 Then
Me!MemberID.Text = Left(Me!MemberID.Text, Len(Me!MemberID.Text) - 1)
End If
' automatically increment the member number. The member number is
' formed by using the church number and adding a sequence number,
' ex., 08930008. Get the church number after user enters 4 numbers,
' find the highest number used for that year, and then add 1.
' If starting anew, add 0001. (This allows a church to have a maxium
' of 9999 members.)
' the user types the first 4 characters, which represents the
' church number
If Len(Me!MemberID.Text) = 4 Then
' if church number typed is different than default church
' number, then tell the user
If Me!MemberID.Text <> Right("000" & DLookup("Church", _
"Defaults"), 4) Then
If vbNo = MsgBox("This is not the default church ID! " _
& "Do you wish to continue", vbQuestion + vbYesNo, "Warning!")
Then
Exit Sub
End If
End If
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
' built query string to sort MemberID in descending oder
' where the first part of MemberID matches characters typed
strSQL = "SELECT TOP 1 MemberID FROM Members "
strSQL = strSQL & "WHERE (((Left([MemberID], 4)) = """
strSQL = strSQL & Me!MemberID.Text & """))"
strSQL = strSQL & "ORDER BY MemberID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!MemberID.Text = Me!MemberID.Text & "0001"
Else
rst.MoveFirst
Me!MemberID.Text = Right("000" & (rst(0) + 1), 8)
End If
DoCmd.GoToControl "FirstName"
rst.Close
Set rst = Nothing
Set db = Nothing
End If
Exit_MemberID_Change:
Exit Sub
Err_MemberID_Change:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_Members Form." & "MemberID_Change")
Resume Exit_MemberID_Change
End Select
End Sub
2ND ONE:
Option Compare Database
Option Explicit
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
On Error GoTo Err_Form_BeforeInsert
Set db = DBEngine(0)(0)
' built query string to sort AddresID in descending oder
' where the first part of AddresID matches default church number
strSQL = "SELECT TOP 1 AddresID FROM KbyAngAlamat"
strSQL = strSQL & "WHERE (((Left([AddresID], 4)) = """
strSQL = strSQL & Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & """))"
strSQL = strSQL & "ORDER BY AddresID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!AddresID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & "0001"
Else
rst.MoveFirst
' add 1 to AddresID and add leading zeros
Me!AddresID = Right("000" & (rst(0) + 1), 4)
' combine default church number with sequence number
' from previous line
Me!AddresID = Right("000" & Nz(DLookup("Church", _
"Defaults"), "9999"), 4) & Me.AddresID
End If
DoCmd.GoToControl "HOUSEHOLDNAME"
rst.Close
Set rst = Nothing
Set db = Nothing
Exit_Form_BeforeInsert:
Exit Sub
Err_Form_BeforeInsert:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_ALAMAT PER KELUARGA_JEMAAT KBY Form." & "Form_BeforeInsert")
Resume Exit_Form_BeforeInsert
End Select
End Sub
Private Sub AddresID_Change()
' examine each character as it is entered and if it is not an acceptable
' character then ignore it and leave string as it was before
On Error GoTo Err_AddresID_Change
If InStr(1, "0123456789", Right(Me!AddresID.Text, 1)) = 0 Then
Me!AddresID.Text = Left(Me!AddresID.Text, Len(Me!AddresID.Text) - 1)
End If
' automatically increment the AddresID number. The AddresID number is
' formed by using the church number and adding a sequence number,
' ex., 08930008. Get the church number after user enters 4 numbers,
' find the highest number used for that year, and then add 1.
' If starting anew, add 0001. (This allows a church to have a maxium
' of 9999 AddresID.)
' the user types the first 4 characters, which represents the
' church number
If Len(Me!AddresID.Text) = 4 Then
' if church number typed is different than default church
' number, then tell the user
If Me!AddresID.Text <> Right("000" & DLookup("Church", _
"Defaults"), 4) Then
If vbNo = MsgBox("This is not the default church ID! " _
& "Do you wish to continue", vbQuestion + vbYesNo, "Warning!")
Then
Exit Sub
End If
End If
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
' built query string to sort AddresID in descending oder
' where the first part of AddresIDmatches characters typed
strSQL = "SELECT TOP 1 AddresID FROM KbyAngAlamat "
strSQL = strSQL & "WHERE (((Left([AddresID], 4)) = """
strSQL = strSQL & Me!AddresID.Text & """))"
strSQL = strSQL & "ORDER BY AddresID DESC;"
'DoCmd.Hourglass True
Set rst = db.OpenRecordset(strSQL)
'DoCmd.Hourglass False
If rst.BOF Then
MsgBox "Initial entry!"
Me!AddresID.Text = Me!AddresID.Text & "0001"
Else
rst.MoveFirst
Me!AddresID.Text = Right("000" & (rst(0) + 1), 8)
End If
DoCmd.GoToControl "HOUSEHOLDNAME"
rst.Close
Set rst = Nothing
Set db = Nothing
End If
Exit_AddresID_Change:
Exit Sub
Err_AddresID_Change:
Select Case Err.Number
Case Else
'Call LogError(Err.Number, Err.Description, _
"Form_ALAMAT PER KELUARGA_JEMAAT KBY Form." & "AddresID_Change")
Resume Exit_AddresID_Change
End Select
End Sub