MSAccess Connection string not working

Discussion in 'C#' started by Supremus, Oct 8, 2006.

  1. #1
    G'day,

    Whilst trying to solve another problem on my web site, I discovered that this code doesn't do what I would expect.

    <%
    Datpath=Server.MapPath("hits.mdb")
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& datpath
    sqlstmt = "select * from hits"
    Set rs = Server.CreateObject("ADODB.Recordset")
    rs.Open sqlstmt, conn, adOpenDynamic

    response.write "<br /> "
    response.write sqlstmt
    response.write " <br />"

    response.write "Record Count = "
    response.write rs.recordcount
    response.write "<br />***"
    set conn = nothing
    %>

    It returns this result ...

    select * from hits
    Record Count = -1
    ***

    But the hits.mdb contains data so I should not have -1
    (Hits is the name of the database and the table.)

    What have I done wrong.
     
    Supremus, Oct 8, 2006 IP
  2. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't think recordcount gets populated until you do a move, and even then I seen to recall it's a bity iffy.

    Try "select count(*) as numrecs from hits" instead

    regards
     
    Free Born John, Oct 9, 2006 IP
  3. surfnearnzone

    surfnearnzone Well-Known Member

    Messages:
    1,492
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Its a very minute mistake but it can land you in trouble like this :)

    If you can find the difference then $10 to you. Check out the correction here ;)



    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & datpath

    Nah i should'nt be throwing a wedger here perhaps :p

    Happy coding,

    Surfnearnzone

     
    surfnearnzone, Oct 9, 2006 IP
  4. Supremus

    Supremus Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You mean that space before the & ????

    I'm just about to go to bed but I'll try in the morning.

    Thanks.
     
    Supremus, Oct 9, 2006 IP
  5. surfnearnzone

    surfnearnzone Well-Known Member

    Messages:
    1,492
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #5
    exactly. i spent a lot of time figuring this out and was nuts after i knew it :D i mean when i had started in asp and access
     
    surfnearnzone, Oct 9, 2006 IP
  6. Supremus

    Supremus Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I couldn't go to bed. I had to find out.

    Thanks to you both for fixing my problem.
     
    Supremus, Oct 9, 2006 IP
  7. surfnearnzone

    surfnearnzone Well-Known Member

    Messages:
    1,492
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #7
    You're always welcome and great that it worked for you :D :cool:

     
    surfnearnzone, Oct 9, 2006 IP