Results 1 to 5 of 5
  1. #1
    NejcZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2014
    Posts
    17

    Sql SELECT to variable

    Hi everyone
    I have pdata value as string:



    Code:
    Dim pdata(0 To 29) As String
    For the last value (ID), I want my program to calculate next suitable number. Next ID should be 1 greater then the last one in SQL database, so my SQL query is:

    Code:
    SELECT TOP 1 MAX(VAL(ID)) + 1
    That gives me the number I need.
    How do I get this value to variable? And then assign it to pdata variable for sending it to database along with my other fields (pdata stands for parsed data )

    Code:
    pdata(29) = sql query??
    I've tried some options, but could not get it to work. Any ideas?
    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Is your 'code' the SQL syntax of query object?

    Saving calculated data is usually a bad idea. Why do you need to save this value to table? Are you trying to generate a unique ID?

    Why have you declared the variable with (0 To 29)? I've never seen that.

    What exactly are you trying to accomplish?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    NejcZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2014
    Posts
    17
    AutoNumber system does not work for me, so I want just a simple line of code, that generates next ID.
    0-29 pdata variables is for parsed data, I have 28 lines which I parse using RegEx, and 1 for ID. I'm not even sure what is the right way, so basicly what I need to do is:
    -get last ID value from my database
    -add 1 to that value
    -save new value back to database along with other new data (new entry in database)

    So you're right. I want to calculate unique ID, which is for 1 grater than last ID.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    What do you mean by 'sending it to database along with my other fields'?

    Are you doing data entry with a form?

    Is this a multi-user db?

    If not, can try textbox DefaultValue property:

    DMax("ID", "tablename") + 1
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    NejcZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2014
    Posts
    17
    I'm doind data entry from file. I use RegExp to parse data I need and save it to database. So far, I was setting ID by hand, but becouse I want some numbers to auto-generete depending on ID, i realy need this to be done automaticly. Database is used by only 1 person at the time.

    Top work! Just tried your suggestion and works a treat
    If anyone is looking for solution:

    Code:
    Dim MyValue As String
        MyValue = DMax("ID", "Garancije") + 1
        
        pdata(29) = MyValue
    Last edited by NejcZ; 10-04-2014 at 01:28 AM. Reason: wrong code sample

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-25-2012, 02:53 AM
  2. Replies: 5
    Last Post: 05-18-2012, 07:31 AM
  3. How to use variable in SELECT DISTINCT
    By celtics11 in forum Access
    Replies: 1
    Last Post: 11-18-2011, 04:28 PM
  4. select statement with variable table name
    By dv89k in forum Queries
    Replies: 1
    Last Post: 05-26-2011, 10:54 PM
  5. SELECT INTO variable table name
    By Ian P in forum Queries
    Replies: 2
    Last Post: 05-29-2010, 12:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums