Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP Address into MS Access database

Status
Not open for further replies.

SteveHigh

Technical User
Jan 17, 2007
158
GB
Hello

I have an MS Access database with two fields: VisitorID and IPAddress (this latter is a text data field and accepts zero-length strings).

I have the following code in my <head></head> tags and do not get an error message.

The database table (called sIPAddresses) does increment by one in the VisitorID field each time a visitor clicks on the site, but it does NOT insert the IPAddress in the adjacent column.

Any ideas, please?

Many thanks.

Steve

<%
' Declare variablesDim sIPAddresssIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

If sIPAddress="" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")

'Open MS Access database, store form field valuesset

conn=Server.CreateObject("ADODB.Connection")conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\nameofform.mdb;"

set rs = Server.CreateObject("ADODB.recordset")

SQL="INSERT INTO sIPAddresses (IPaddress) VALUES ('" & _IPaddress & "')"

rs.Open SQL,
connSet rs=Nothing

' Close the connection

conn.CloseSet conn=Nothing
%>
 
I don't see anything wrong with your code. Are you sure your collecting valid IP Addresses? Try Response.Write'ing your SQL string so you can see what it is actually writing.

 
Hello Tarwn

Thank you for your message.

I am not sure how to do that, sorry. Do you mean use Response.Write in the actual asp file? And where would I insert Response.Write?

I have searched on the Web, but the explanations are not too clear.

Many thanks again, Tarwn.

Steve
 
Shouldn't this:

SQL="INSERT INTO sIPAddresses (IPaddress) VALUES ('" & _IPaddress & "')"

be this:

SQL="INSERT INTO sIPAddresses (IPaddress) VALUES ('" & sIPaddress & "')"

because

sIPAddress = Request.ServerVariables("REMOTE_ADDR")


I do not see a variable set for _IPaddress
 
i see the probable fix action...to anser the other question...

Code:
<%
' Declare variablesDim sIPAddresssIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

If sIPAddress="" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")

'Open MS Access database, store form field valuesset

conn=Server.CreateObject("ADODB.Connection")conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\nameofform.mdb;"

set rs = Server.CreateObject("ADODB.recordset")

' sql debug...remove comments to test
' Response.Write "INSERT INTO sIPAddresses (IPaddress) VALUES ('" & IPaddress & "')"
' Response.End



SQL="INSERT INTO sIPAddresses (IPaddress) VALUES ('" & _IPaddress & "')"

rs.Open SQL, 
connSet rs=Nothing

' Close the connection

conn.CloseSet conn=Nothing
%>
 
You have all been most helpful - thank you.

bslintx, thanks for letting me know how best to script the Response.Write statements. I have saved your post and will use it for future reference.

Mbiro's SQL="INSERT INTO sIPAddresses (IPaddress) VALUES ('" & sIPaddress & "')"

worked! The IP address is now stored in the database. Many thanks.

Just for your own information, I was informed elsewhere that I could not use rs in this type of INSERT and that I needed to use: conn.Execute(SQL). This worked, too.

Cheers

Steve




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top