Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Access SQL vba question

  • 08-07-2009 5:23pm
    #1
    Registered Users, Registered Users 2 Posts: 455 ✭✭


    Hi.

    I hope someone can help with a question about Access SQL. To fill some of my spare time, which I have lots of at the moment, I am trying to teach myself some basic SQL with MS Access 2007.

    I'm trying to populate table with 'INSERT INTO'. At present I just have 2 fields in database which are [ID] and [CodeNumber]. ID is primary key.

    I'm trying to create 100 records with simple serial number ID field and then a code number which is created elsewhere in the code. This is what I did:

    Dim mySQL2 As String
    Dim xx As Integer

    For xx = 1 To 100

    mySQL2 = "INSERT INTO test"
    mySQL2 = mySQL2 & " (ID, [codeNumber])"
    mySQL2 = mySQL2 & " VALUES ( xx, 'Codefromelsewhere')"

    DoCmd.RunSQL mySQL2

    Next xx


    The value 'xx' won't work. I assume I need something to identify this as a variable from the loop. At present when I run the form, it asks me to enter a value for 'x'.

    Make sense?
    Thanks in advance


Comments

  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    maximus02 wrote: »
    Dim mySQL2 As String
    Dim xx As Integer

    For xx = 1 To 100

    mySQL2 = "INSERT INTO test"
    mySQL2 = mySQL2 & " (ID, [codeNumber])"
    mySQL2 = mySQL2 & " VALUES ( xx, 'Codefromelsewhere')"

    DoCmd.RunSQL mySQL2

    Next xx


    The value 'xx' won't work. I assume I need something to identify this as a variable from the loop. At present when I run the form, it asks me to enter a value for 'x'.

    I'm a bit lost here, not sure about using variables in SQL - however in SQL isnt the % sign used to request input from user


  • Closed Accounts Posts: 5,096 ✭✭✭--amadeus--


    I'm not able to test this at the minute (cooking dinner!) but you don't need to identify xx as a variable to the SQL as such. Essentially what you are doing is creating a text string variable that happens to be a piece of SQL code and then executing it.

    However I think your error is that it is treating xx as part of teh string rtaher than a variable in it's own right. Try:

    mySQL2 = mySQL2 & " VALUES ( " & xx & ", 'Codefromelsewhere')"

    the VBA is treating everything inside the " " as part of teh text string so by breaking it it can see the variable.


  • Registered Users, Registered Users 2 Posts: 455 ✭✭maximus02


    Amadeus

    Thanks a lot. That worked fine.

    Of course it all makes sense in hindsight. I should have known that.

    Thanks again


  • Closed Accounts Posts: 5,096 ✭✭✭--amadeus--


    hindsight is *always* 20:20, the obvious stuff is often teh hardest to see!


Advertisement