Question : Updating a table from VBA

I am trying to generate a unique identifier for each Entity. This I have managed but I wish to store z in the Table "ID_Generator" as the last used identifier.
The Table has two records and based on whether it is a Company or an Individual. The code also updates a form which works fine.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Sub DIR_CTP_IDGenerator()
Dim x, z As Integer

x = DLookup("[EntityID]", "ID_Generator ", "[EntityType] = 'Company'")
z = x + 1
If DLookup("[EntityID]", "ID_Generator ", "[EntityType] = 'Company'") Then
DLookup("[EntityID]", "ID_Generator ", "[EntityType] = 'Company'") = z
End If
If z < 10 Then
[Forms]![CTP Details]![CID] = "C000" & z
End If
If z >= 10 And z < 100 Then
[Forms]![CTP Details]![CID] = "C00" & z
End If

If z >= 100 And z < 1000 Then
[Forms]![CTP Details]![CID] = "C0" & z
End If

If z >= 1000 Then
[Forms]![CTP Details]![CID] = "C" & z
End If


End Sub

Answer : Updating a table from VBA

Ooops try

DoCmd.RunSQL "UPDATE ID_Generator SET EntityID = " & z & " WHERE EnitityType = 'Company'"

 

Replace

If DLookup("[EntityID]", "ID_Generator ", "[EntityType] = 'Company'") Then
DLookup("[EntityID]", "ID_Generator ", "[EntityType] = 'Company'") = z
End If

with my one line

Kelvin

Random Solutions  
 
programming4us programming4us