简体   繁体   中英

SQL Syntax in MS Access Form

I have a problem of SQL syntax which I have been struggling for days.

I have created an MS Access 2010 Form called IP Country Form into which I want to insert into a TextBox called Country the Country obtained from the following query which queries a table called IPLocation which contains the Countries according to IP Numbers with the fields:

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

The table rows looks like this:

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

The form calculates the IP Number from the IP address and places it in a TextBox called IPNumber , which I then use in an SQL Query to obtained the Country which corresponds to it.

My current query is:

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]))

IPNS, IPNE are Numbers as is IPNumber

Essentially the query is designed to find the Country where the IPNumber lies between IPNS and IPNE.

Any help would be much appreciated.

A test query I use:

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))
           ")

Works fine and returns the correct country

On the query you use on your code you have the comparison operators reversed on the AND clause.

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

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

Should be:

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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM