Results 1 to 9 of 9

Thread: sql problem with auto number field [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Resolved sql problem with auto number field [RESOLVED]

    This is the start of my sql statement, but I am having problems because the first field in the db is an autonumber and I don't know how to bypass it in the sql (2nd line). I have tried just a , but that doesn't work.



    SQL="INSERT INTO Players VALUES(" & _
    "," & _
    "'" & Request.Form("username") & "'," & _
    Last edited by Supremus; Dec 10th, 2006 at 04:22 PM.
    My reputation is in tatters. Don't bother trying to repair it.

  2. #2
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Up State NY
    Posts
    525

    Re: sql problem with auto number field

    Dont refrence the field as you are inserting data, access will fill in that field for you. Try this:
    <code>
    Sql="insert into Players (username, password {, add other column names})values("'" & Request.Form("username") & "'," & )
    </code>


    Good luck, Julie (dislocated sydneysider)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: sql problem with auto number field

    Thanks Julie,

    I was hoping there was a way without listing all 30+ fields by just using the value.

    If I put a 0 in the first field I get ...
    The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

    If I just use a comma like this
    SQL="INSERT INTO KQPlayers VALUES(" & _
    "," & _
    "'" & Request.Form("username") & "'," & _

    to bypass it, I get
    Syntax error in INSERT INTO statement.
    My reputation is in tatters. Don't bother trying to repair it.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: sql problem with auto number field

    If I just use
    Sql="insert into Players (username) values('" & Request.Form("username") & "'" )
    I get

    Error: Missing Fields
    You must enter all fields to proceed.
    My reputation is in tatters. Don't bother trying to repair it.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: sql problem with auto number field

    What happens if you specify all fields except the autonumber one? (it should be fine then)

    Even tho it takes more text, specifying all fields is a good idea as it protects you against any table changes..
    • If the fields you use still exist but the order has changed, it will still work as you originally inteded;
    • if other fields have been added (after or between the existing fields) it will still work as intended;
    • if fields you use have been removed, you will get an error from the statement, rather than accidentally putting data into the wrong fields.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: sql problem with auto number field

    Thanks Si, I'll try that. I thought you were able just to list the ones you wanted changed.

    Maybe it's the Insert Into command causeing my problems. Maybe it would be simpler just to add a new record?
    My reputation is in tatters. Don't bother trying to repair it.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: sql problem with auto number field

    I'm getting frustrated again. I have removed the autonumber to make things simpler. I'll add it later when I can solve this problem ...

    The following code works fine and adds the records and the data looks right ...

    SQL="INSERT INTO KQPlayers VALUES(" & _
    "'" & Request.Form("username") & "'," & _
    "'" & RandPassword & "'," & _
    "'" & Request.Form("email") & "'," & _
    (--snip--)

    But as soon as I try to add this after KQPlayers and before Values, I get a syntax error. The datafield names are correct as I cut/pasted them.

    (Username, Password, Email, CurLevel, ABWin, ABLose, ABTie, DBWin, DBLose, DBTie, Exp, Money, DateCreated, DateEdited, NumLogins, LastDefeated, LastDefeatedTime, LastDefeatedBy, LastDefeatedByTime, LastTied, LastTiedTime, Salary, AttacksAccumulated, DailyAttacks, MaxUnits, Allocation, UnreadAnnouncements, TrainerAccumulated, DailyTrainer, Luck, Food, LastTimeOn)
    My reputation is in tatters. Don't bother trying to repair it.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: sql problem with auto number field

    That's a problem with some of the field names you have used - they are reserved words (names of data types, etc), so should not be field names. The ones likely to be causing issues are:
    Username, Password, Email, Money, Allocation
    Ideally you should change the field names, as they may cause problems in other ways.

    If that isn't possible you can use a 'temporary' fix in your SQL statements, which is to put them in square brackets, eg:
    Code:
    ([Username], [Password], ...
    I thought you were able just to list the ones you wanted changed.
    You can (assuming you fill all fields that have been set as required in the table design), but you should always specify the fields, not just assume that the positions within the Values clause will map to the correct fields.
    Last edited by si_the_geek; Dec 10th, 2006 at 10:45 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: sql problem with auto number field

    Thanks a lot Si.
    My reputation is in tatters. Don't bother trying to repair it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width