2014-02-27 63 views
0

我將解釋我的情況,希望能夠解釋我正在嘗試做什麼。我有一張桌子上有我們的員工電話號碼,但我需要能夠看到接下來的5個可用號碼。在Access 2010中添加x條記錄

我有tbl_ext,它是當前的數字和tbl_temp,用於根據狀態輸入「開始」數字(管理員獲取1xxx,銷售獲得2xxx等)。我需要知道是否可以對未在tbl_ext中的5個數字進行「計數」並將其插入到tbl_temp中。

希望這會有所幫助,因爲我開始懷疑它是否可能。

回答

0

以下代碼將爲以'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 
+0

謝謝你的代碼。這是我需要的。至於你提出的問題,這些字段被設置爲數字,它只是我後面的5位數的擴展名,不需要擔心其他信息。作爲範圍,有7個不同的「狀態」級別,所以這將設置爲7個不同的級別。 –

+0

由於我不知道7個級別是全部在一起(1-7)還是1-5,8,9,我將更新代碼,以便它可以處理多達9個分組。我會放置一個「IF」跳過一個不存在的號碼,這樣你就可以修改該IF以適應你的需求。 –

相關問題