用下面的函数能实现,但我想用一句SQL语句,不用中间表,实现以下函数的功能。
Public Sub All_SubID(P_ID As Integer)
Dim Str1 As String
Str1 = "SELECT CategoryID INTO LSB FROM GoodsCategory WHERE ParentID=" + CStr(PID)
DoCmd.SetWarnings False
DoCmd.RunSQL Str1
Dim W1 As Boolean
W1 = True
While W1
Str1 = "SELECT CategoryID INTO LSB1 FROM GoodsCategory WHERE ParentID IN (SELECT CategoryID FROM LSB) AND CategoryID NOT IN (SELECT CategoryID FROM LSB)"
DoCmd.RunSQL Str1
If DCount("CategoryID", "LSB1") > 0 Then
Str1 = "INSERT INTO LSB (CategoryID) SELECT CategoryID FROM LSB1"
DoCmd.RunSQL Str1
Else
W1 = False
End If
Wend
End Sub