2012-09-21 88 views
1

我有一個SQL語法問題,我一直在努力工作幾天。MS Access中的SQL語法

我已經創建了一個MS Access 2010中的表單叫IP Country Form到我要插入一個名爲Country從下列查詢該查詢的表叫IPLocation根據IP號碼與字段包含的國家獲得國家文本框:

IPNS IPNE CY2(2 letter Country Code) CY3(3 Letter country Code) Country (full Country Name) 

表中的行看起來是這樣的:

IPNS  | IPNE  | CY2 | CY3 | COUNTRY 
19791872 | 19922943 | TH | THA | THAILAND 

形式從IP地址,並將其放置在一個名爲文本框計算IP號碼,然後我在SQL查詢中使用它來獲取與之相對應的國家/地區。

我當前的查詢是:

SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocationCY3, IP.Location.Country 
    FROM IPLocation 
WHERE (((IPLocation.IPNS)<" & [Forms]![IP Country Form]![IPNumber]) 
    And ((IPLocation.IPNE) > " & [Forms]![IP Country Form]![IPNumber])) 

脈衝中子源,IPNE是數字作爲是IPNumber

本質上查詢的目的是尋找到IPNumber位於IPNS和IPNE之間的國家。

任何幫助將不勝感激。

測試查詢使用:

Set rst = dbs.OpenRecordset(" 
       SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocation.CY3, 
         IPLocation.Country 
        FROM IPLocation 
        WHERE (((IPLocation.IPNS)>19791871) 
        AND ((IPLocation.IPNE)<19922944)) 
      ") 

工作正常,並返回正確的國家

+1

你< and >在你測試查詢是相反的。 –

+0

是什麼問題? – Ghost

回答

0

在你在你的代碼中使用您們改變了對AND條款比較運營商查詢。

IPLocation.IPNS)>" & [Forms]![IP Country Form]![IPNumber]

IPLocation.IPNE)<" & [Forms]![IP Country Form]![IPNumber]

應該是:

SELECT IPLocation.IPNS, IPLocation.IPNE, IPLocation.CY2, IPLocationCY3, 
     IP.Location.Country 
    FROM IPLocation 
WHERE (((IPLocation.IPNS) > " & [Forms]![IP Country Form]![IPNumber]) 
    And ((IPLocation.IPNE) < " & [Forms]![IP Country Form]![IPNumber])) 
+0

大家好,我的問題是當我在一個過程中使用語句運行查詢:Set rst = dbs.OpenRecordset(「SELECT CY2,CY3,Country FROM IPLocation WHERE IPNS <」&[Forms]![IP Country Form]! [IPNumber]和IPNE>「&[Forms]![IP Country Form]![IPNumber]」)。編譯錯誤變量IPNE未定義。 – user1687946

+0

請編輯您的問題以添加錯誤和您可以提供的任何新信息。正如你所看到的,評論上的代碼不能很好地工作。 – Yaroslav