从其他表访问Vba序列ID

时间:2017-04-05 05:06:12

标签: access-vba sequence access

我有两张桌子,ID表和Mastertable。在ID表中,我有水平和活动,给出一个起始编号,让我们说1000.在Mastertable中,我有许多与相同活动和水平相关的图纸。我必须从ID表值1000开始为它们提供编号并将其递增1.完成后,必须将mastertable的最大值重新插入ID表。

此外,如果有Level和Activity,则必须选择ID来查找级别和活动,如果没有提到级别,那么它必须只查找活动。

我尝试了很多但是没有成功。

我使用下面的代码,但它只查找活动而不是水平。此外,它不会返回到id表并从主表更新最大ID。

Option Compare Database
Option Explicit

Public Function SequenceNew()
Dim strSQL As String
Dim db As Database
Dim rs As DAO.Recordset
Dim a, initNo As Integer
Dim b As Integer


strSQL = "SELECT * FROM MasterTable ORDER BY LevelID"
'Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF

rs.Edit
If rs![DrawingTypeName] = "Concrete" And rs![ProjectName] = Forms!frm_Publish!CboProject And rs!IDGiven = "Not Given" Then
a = a + 1
rs!Sequence = DLookup("CONCRETE", "Qry_ID_Selected") + a

ElseIf rs![DrawingTypeName] = "Reinforcement" And rs![ProjectName] = Forms!frm_Publish!CboProject And rs!IDGiven = "Not Given" Then
b = b + 1
rs!Sequence = DLookup("REINFORCEMENT", "Qry_ID_Selected") + b

ElseIf rs![DrawingTypeName] = "Steel structural works" And rs![ProjectName] = Forms!frm_Publish!CboProject And rs!IDGiven = "Not Given" Then

End If
rs.Update
rs.MoveNext
Loop
rs.Close
Set db = Nothing
Else
MsgBox " No records Found"
rs.Close
Set db = Nothing
End If

End Function

应该有其他更好的方法来做到这一点。 MasterTable ID Table

1 个答案:

答案 0 :(得分:1)

问题已解决,我可以解决新代码。

Option Compare Database
Option Explicit

Public Function SequenceNewLevel()
Dim strSQL As String
Dim strSQL1 As String
Dim db As Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset2
Dim a, initNo As Integer
a = 0
strSQL = "SELECT * FROM MasterTable"
strSQL1 = "SELECT * FROM ID"
Set db = CurrentDb
Set rs1 = CurrentDb.OpenRecordset(strSQL1)
If rs1.RecordCount > 0 Then
rs1.MoveFirst
Do While Not rs1.EOF
    rs1.Edit
    Set rs = CurrentDb.OpenRecordset(strSQL)
    If rs.RecordCount > 0 Then
    rs.MoveFirst
    Do While Not rs.EOF
    rs.Edit
        If rs![TypeOfDrawing] = rs1![Activity] And rs![Project] = rs1![Project] And rs![LevelName] = rs1![Level] And rs![IDGiven] = "Not Given" Then
        a = a + 1
        rs!Sequence = rs1!StartID + a
        rs1!StartID = rs!Sequence
        a = 0
        rs![IDGiven] = "GIVEN"
        End If
    rs.Update
    rs.MoveNext
    Loop
End If
 rs1.Update
rs1.MoveNext
Loop
rs1.Close
rs.Close
Set db = Nothing
Else
MsgBox " No records Found"
rs.Close
Set db = Nothing
End If
End Function


Public Function SequenceNewWithOutLevel()
Dim strSQL As String
Dim strSQL1 As String
Dim db As Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset2
Dim a, initNo As Integer
a = 0
strSQL = "SELECT * FROM MasterTable"
strSQL1 = "SELECT * FROM IDWithoutlevel"
Set db = CurrentDb
Set rs1 = CurrentDb.OpenRecordset(strSQL1)
If rs1.RecordCount > 0 Then
rs1.MoveFirst
Do While Not rs1.EOF
    rs1.Edit
    Set rs = CurrentDb.OpenRecordset(strSQL)
    If rs.RecordCount > 0 Then
    rs.MoveFirst
    Do While Not rs.EOF
    rs.Edit
        If rs![TypeOfDrawing] = rs1![Activity] And rs![Project] = rs1![Project] And rs![IDGiven] = "Not Given" Then
        a = a + 1
        rs!Sequence = rs1!StartID + a
        rs1!StartID = rs!Sequence
        a = 0
        rs![IDGiven] = "GIVEN"
        End If
    rs.Update
    rs.MoveNext
    Loop
End If
 rs1.Update
rs1.MoveNext
Loop
rs1.Close
rs.Close
Set db = Nothing
Else
MsgBox " No records Found"
rs.Close
Set db = Nothing
End If
End Function