以下代碼將爲以'1xxx'開頭的擴展創建接下來的五個數字。你沒有提到該字段的格式(文本或數字),或者如果你存儲區號+交換+分機。 你也沒有注意到有多少不同的「狀態」(1,2,3,...)。您可以克隆代碼並重復1-9,或者,您可以選擇所有分機的唯一高位數字。當您處理更多細節時,我可以更新答案。
Public Function Create_Phone_Numbers()
Dim db As Database
Dim rst As Recordset
Dim strSQL As String
Dim i As Integer
Dim iLoop As Integer
Dim iExt As Integer
Dim iStatus As Integer
Set db = CurrentDb
For iStatus = 1 To 9
If iStatus = 999 Then ' Change this line to skip unused numbers
' Do nothing - skip this
Else ' It's a number we want.
strSQL = "SELECT TOP 1 tbl_Ext.PhoneExt FROM tbl_Ext GROUP BY tbl_Ext.PhoneExt HAVING (((tbl_Ext.PhoneExt) Like '" & iStatus & "*')) ORDER BY tbl_Ext.PhoneExt DESC;"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not (rst.EOF And rst.BOF) Then
iExt = rst!PhoneExt
For i = 1 To 5
strSQL = "insert into tbl_Temp (PhoneExt) Select " & iExt + i & " As Expr1;"
db.Execute strSQL
Next i
End If
End If
Next iStatus
rst.Close
Set rst = Nothing
db.Close
Set db = Nothing
End Function
謝謝你的代碼。這是我需要的。至於你提出的問題,這些字段被設置爲數字,它只是我後面的5位數的擴展名,不需要擔心其他信息。作爲範圍,有7個不同的「狀態」級別,所以這將設置爲7個不同的級別。 –
由於我不知道7個級別是全部在一起(1-7)還是1-5,8,9,我將更新代碼,以便它可以處理多達9個分組。我會放置一個「IF」跳過一個不存在的號碼,這樣你就可以修改該IF以適應你的需求。 –