Next Number and Unused Registration Number

  • Thread starter Frank Situmorang
  • Start date
F

Frank Situmorang

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

Also should I put the function in the module of the database or leave it as
a sub?
--
H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Frank,
Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

The "***" is how I show new (added) lines.

The comments ' 1, ' 2 ,... is how I keep track of nested "If...ELSE...END
IF" statements.

Example:
IF Then ' 1
IF Then '2
IF Then ' 3
End IF '3
End if ' 2
Else ' 1

End if ' 1
Also should I put the function in the module of the database or leave it as
a sub?

Your code is a SUB. Replace your current Sub with the one in my post.


Mine is a FUNCTION. It MUST stay as a Function.

Paste the "FUNCTION GetNumber()" below the Sub AngtType_AfterUpdate()

Just like you see it in the post.

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)



H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Sorry. This is the correct function:

'----------------------Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.NoInd + 1 as NN,"
sSQL = sSQL & " (select min(t.NoInd) - 1 from bukuangkby as t where
t.NoInd > bukuangkby.NoInd)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.NoInd from bukuangkby as t
where t.NoInd = bukuangkby.NoInd + 1)"
sSQL = sSQL & " AND bukuangkby.NoInd< (select Max(s.NoInd)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By NoInd;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------------------------


Sorry
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Steve Sanford said:
Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Steve,

NoInd is the name of the control, while NOIN is the field name.

Pls. disregard the workd "name", that is not the name of the actual field, I
just want to illustrate, the requirement of what I meant reusing the number
if in the middle of the road a clerck changed the member ype( AngtType)

I will try to put your codes


Thanks a lot for your help.


--
H. Frank Situmorang


Steve Sanford said:
Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Frank,
NoInd is the name of the control, while NOIN is the field name.

Then this is the correct code to use:
(watch for line wrap)

'--------Correct code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber1()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t "
sSQL = sSQL & " Where t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " Where not exists (select t.noin from bukuangkby as t "
sSQL = sSQL & " Where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< "
sSQL = sSQL & " (select Max(s.noin)+1 from bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------Correct code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Steve,

NoInd is the name of the control, while NOIN is the field name.

Pls. disregard the workd "name", that is not the name of the actual field, I
just want to illustrate, the requirement of what I meant reusing the number
if in the middle of the road a clerck changed the member ype( AngtType)

I will try to put your codes


Thanks a lot for your help.


--
H. Frank Situmorang


Steve Sanford said:
Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


:

Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Steve,

When I copied then pasted to replace the oldone, it shows red color fonts on
this:
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4

Anything wrong?

Thanks in advance
--
H. Frank Situmorang


Steve Sanford said:
Frank,
NoInd is the name of the control, while NOIN is the field name.

Then this is the correct code to use:
(watch for line wrap)

'--------Correct code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber1()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t "
sSQL = sSQL & " Where t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " Where not exists (select t.noin from bukuangkby as t "
sSQL = sSQL & " Where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< "
sSQL = sSQL & " (select Max(s.noin)+1 from bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------Correct code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Steve,

NoInd is the name of the control, while NOIN is the field name.

Pls. disregard the workd "name", that is not the name of the actual field, I
just want to illustrate, the requirement of what I meant reusing the number
if in the middle of the road a clerck changed the member ype( AngtType)

I will try to put your codes


Thanks a lot for your help.


--
H. Frank Situmorang


Steve Sanford said:
Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:


Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


:

Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
 
F

Frank Situmorang

Steve,

When I tried to test it, here is the error:

Private Sub AngtType_AfterUpdate()
'--------Correct code-----------------
If AngtType = "Anggota Jemaat" Then ' 1
NoInd = GetNextNumber
This:private Sub AngtType_AfterUpdate()yellow color and it hangs and
highlighted in GetNextNumber

Thanks for your help to solve the above problem

--
H. Frank Situmorang


Steve Sanford said:
Frank,
Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

The "***" is how I show new (added) lines.

The comments ' 1, ' 2 ,... is how I keep track of nested "If...ELSE...END
IF" statements.

Example:
IF Then ' 1
IF Then '2
IF Then ' 3
End IF '3
End if ' 2
Else ' 1

End if ' 1
Also should I put the function in the module of the database or leave it as
a sub?

Your code is a SUB. Replace your current Sub with the one in my post.


Mine is a FUNCTION. It MUST stay as a Function.

Paste the "FUNCTION GetNumber()" below the Sub AngtType_AfterUpdate()

Just like you see it in the post.

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)



H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Thanks Steve it is OK now. I just changed it to GetNextNumber1 to be
consistent with the others.

My question is in case we found 3 or 4 last records was wrongly entered, and
we change it, Shall it propose to use all them in the future entry?

Thanks in advance

Greetings from Jakarta, Indonesia
--
H. Frank Situmorang


Steve Sanford said:
Frank,
Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

The "***" is how I show new (added) lines.

The comments ' 1, ' 2 ,... is how I keep track of nested "If...ELSE...END
IF" statements.

Example:
IF Then ' 1
IF Then '2
IF Then ' 3
End IF '3
End if ' 2
Else ' 1

End if ' 1
Also should I put the function in the module of the database or leave it as
a sub?

Your code is a SUB. Replace your current Sub with the one in my post.


Mine is a FUNCTION. It MUST stay as a Function.

Paste the "FUNCTION GetNumber()" below the Sub AngtType_AfterUpdate()

Just like you see it in the post.

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)



H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
F

Frank Situmorang

Steve,

I fortgot to ask you this.

Can I block the control NoInd ( Registration No) to avoid user to fill it
manually?

Thanks in advance for your advice
--
H. Frank Situmorang


Steve Sanford said:
Frank,
Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

The "***" is how I show new (added) lines.

The comments ' 1, ' 2 ,... is how I keep track of nested "If...ELSE...END
IF" statements.

Example:
IF Then ' 1
IF Then '2
IF Then ' 3
End IF '3
End if ' 2
Else ' 1

End if ' 1
Also should I put the function in the module of the database or leave it as
a sub?

Your code is a SUB. Replace your current Sub with the one in my post.


Mine is a FUNCTION. It MUST stay as a Function.

Paste the "FUNCTION GetNumber()" below the Sub AngtType_AfterUpdate()

Just like you see it in the post.

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)



H. Frank Situmorang


Steve Sanford said:
Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 
S

Steve Sanford

Frank,
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4


These two lines should be on the same line. That is what I meant about the
line wrap.

Put the curser at the end of the first line and press the DELETE button once.

--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Steve,

When I copied then pasted to replace the oldone, it shows red color fonts on
this:
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4

Anything wrong?

Thanks in advance
--
H. Frank Situmorang


Steve Sanford said:
Frank,
NoInd is the name of the control, while NOIN is the field name.

Then this is the correct code to use:
(watch for line wrap)

'--------Correct code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber1()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t "
sSQL = sSQL & " Where t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " Where not exists (select t.noin from bukuangkby as t "
sSQL = sSQL & " Where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< "
sSQL = sSQL & " (select Max(s.noin)+1 from bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------Correct code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Steve,

NoInd is the name of the control, while NOIN is the field name.

Pls. disregard the workd "name", that is not the name of the actual field, I
just want to illustrate, the requirement of what I meant reusing the number
if in the middle of the road a clerck changed the member ype( AngtType)

I will try to put your codes


Thanks a lot for your help.


--
H. Frank Situmorang


:

Do not worry. Your English is much, much better than my Indonesian. :)

You said that the code you posted is working, so I did not want to change it
too much. If you change "Angtype" from "Anggota Jemaat" to "SS Member", the
value in

In table "bukuangkby", it looks like there is a field named "noin". But in
the code, what is "NoInd"? A field or a control? I think it is the field in
the table. So the code will have to be changed. See below.

By the way, "Name" is a reserved word in Access and shouldn't be used for
object names. Also, it is not descriptive - Name of what? A better field name
would be "FamilyName" or "LastName" or "L_Name".

**************
Do you mean to say that we can reuse the number for the next record?

Yes, you can reuse the number.

Since you provided example data, I modified my test database and tested the
code.
If I have the correct field names, the code works.

Here is the code again. Use this code:
(watch for line wrap)

'--------New code-----------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub

Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------New code--------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:


Thanks Steve for your quite addvance VBA, I am affraid that I do not
understand well. Because we are Indonesian not so good in English

Do you mean to say that we can reuse the number for the next record?. I want
to make sure that we have the same meaning, because if I finish this
database, we have more than 100 churches and I want to donate my work to them.

Let me illustrate it.

Chruch clerck filled out the form in Mar 7, 2008 as follows:

Rec.No. NoInd Angtype Name
-------- -------- --------- --------
1 SS Member Boaz
2 1 Anggota Jemaat Sarah
3 2 Anggota Jemaat Elfrida
4. SS Member Henoch
5. 3 Anggota Jemaat Elyana

Later in Marh 9, 2008 Church clerck realized, Alas!! I mischoosed ( I used
combo on this field to choose) Elfrida should have been SS member, than he
changed it what we want is to make it blank or zero again

In March 10, church clerck fills in for the new member Johannes, and the
sistem will propose to use this Noind = 2, therefore the record wil show
Rec.No. NoInd Angtype Name
-------- -------- --------- --------
6. 2 Anggota Jemaat Johannes

Is this the VBA that you gave can do?

Sorry Steve to make you bussy, my expert in fact is Accounting, I am just
self thought on this. Yes... I took 30 year ago basic langguage for short
courxe but I alredy forgot it

Thanks for your help Steve

--
H. Frank Situmorang


:

Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
 
S

Steve Sanford

Frank,

Yes you can. Open the form in design view. Click on the control "NoInd" and
open properties. On the "DATA" tab, set the property LOCKED to "YES".

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Frank Situmorang said:
Steve,

I fortgot to ask you this.

Can I block the control NoInd ( Registration No) to avoid user to fill it
manually?

Thanks in advance for your advice
--
H. Frank Situmorang


Steve Sanford said:
Frank,
Steve, waht is the meaning of ' 1; 1 *** and all '2 ... at the end?

The "***" is how I show new (added) lines.

The comments ' 1, ' 2 ,... is how I keep track of nested "If...ELSE...END
IF" statements.

Example:
IF Then ' 1
IF Then '2
IF Then ' 3
End IF '3
End if ' 2
Else ' 1

End if ' 1
Also should I put the function in the module of the database or leave it as
a sub?

Your code is a SUB. Replace your current Sub with the one in my post.


Mine is a FUNCTION. It MUST stay as a Function.

Paste the "FUNCTION GetNumber()" below the Sub AngtType_AfterUpdate()

Just like you see it in the post.

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)



H. Frank Situmorang


:

Hi Frank,

If I understand right, if AngtType = "SS member", you want to set NoInd = 0.

And you want to reuse the number if you change AngtType from "Anggota
Jemaat" to "SS member".


Here is your modified sub and a function to generate the next number:
(Watch for line wrap)

'--------code beg -------------------
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then ' 1
' NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1 ***
NoInd = GetNextNumber
If Not IsNull(Me.[AngtType].OldValue) Then '2
If Me.[AngtType] <> Me.[AngtType].OldValue Then '3
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then ' 4
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If '4
End If '3
End If ' 2
Else ' 1
'If you want the field NULL, uncomment the this line
' NoInd=NULL
' and comment the the this line
NoInd = 0 '***
End If '1
End Sub


Public Function GetNextNumber()
'Function to generate the next number
Dim sSQL As String
Dim r As DAO.Recordset
Dim s As Byte

'create a recordset
sSQL = "select top 1 bukuangkby.noin + 1 as NN,"
sSQL = sSQL & " (select min(t.noin) - 1 from bukuangkby as t where
t.noin > bukuangkby.noin)"
sSQL = sSQL & " from bukuangkby"
sSQL = sSQL & " where not exists (select t.noin from bukuangkby as t
where t.noin = bukuangkby.noin + 1)"
sSQL = sSQL & " AND bukuangkby.noin< (select Max(s.noin)+1 from
bukuangkby as s)"
sSQL = sSQL & " Order By noin;"

Set r = CurrentDb.OpenRecordset(sSQL)
If r.RecordCount = 1 Then
r.MoveFirst
'return the number
GetNextNumber = r.Fields(0)
Else
' no number - let them know
MsgBox "ERROR! Number not generated!!"
End If

'clean up
r.Close
Set r = Nothing

End Function
'--------code end -------------------


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

"Anggota Jemaat" and for SS member( Sabbath School Member/not baptized yet)
we do not assingn the number ( live it blank/zero).

Thi is the VBA to add the next number if we fill the member type as Hurch
member
Private Sub AngtType_AfterUpdate()
If AngtType = "Anggota Jemaat" Then
NoInd = Nz(DMax("[NoIn]", "bukuangkby")) + 1
End If
If Not IsNull(Me.[AngtType].OldValue) Then
If Me.[AngtType] <> Me.[AngtType].OldValue Then
If MsgBox("Anda telah merobah!!, apakah sengaja mau merobah?",
vbYesNo + vbDefaultButton2 + vbQuestion) = vbNo Then
' Undo the changes
Me.AngtType = Me.AngtType.OldValue
End If
End If
End If
End Sub

My question is what is the VBA if later we find out there is mischoice, it
should have been SS member we want to make it to ZERO.

Since the record now is among many records, what is the VBA so that system
will propose again this unused number is the next fill of church member.

Thanks for any idea provided.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top