Results 1 to 7 of 7

Thread: [RESOLVED] Updating tables?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Resolved [RESOLVED] Updating tables?

    Got a question. I have a function getting the last number in a certain table's field. It gets the last number in the field and displays it. I then have a 1 added to it. It displays the last found number in the field, then I have it display what the next number would be, when having a 1 added to it. I need the number that has a 1 added to it, to be put in a table, or rather update it. How can I take the new number and update the field in the other table with the new number? I have the code in a module right now, if that matters.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Updating tables?

    You will need to execute a SQL INSERT statement.

    SQL Inserts have the following structure
    INSERT INTO tablename (fieldname1,fieldname2,..fieldnamen) VALUES (value1, value2,...valuen)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Updating tables?

    This is my code so far:

    VB Code:
    1. Sub GetLastRaceNumber()
    2. Dim rs As ADODB.Recordset
    3. Dim sSQL As String
    4. Dim nextnum As Integer
    5.  
    6.     Set rs = New ADODB.Recordset
    7.    
    8.     sSQL = "SELECT RaceNumber FROM tblNumberGen"""
    9.    
    10.     rs.Open Source:=sSQL, ActiveConnection:=CurrentProject.Connection, _
    11.             CursorType:=adOpenDynamic, LockType:=adLockOptimistic
    12.    
    13.     rs.MoveLast
    14.     MsgBox "last entrynumber is  " & rs.Fields(0).Value
    15.     nextnum = rs.Fields(0).Value + 1
    16.     MsgBox "next entrynumber would be  " & nextnum
    17.     Set rs = Nothing
    18.    
    19.     'sSQL = "UPDATE tblNumberLog.Next INNER JOIN tblEntrants, tblNumberGen ON tblNumberGen.EntryNumber = tblEntrants.EntryNumber, tblEntrants.Category = tblNumberLog.Category SET tblNumberGen.RaceNumber + 1 = tblNumberLog.Next """
    20.     sSQL = "INSERT INTO tblNumberLog (Next) VALUES (nextnum)"""
    21.     'rs.Close
    22.     Set rs = Nothing
    23. End Sub

    The second sSql statement really doesn't do anything. Any ideas?

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Updating tables?

    This may be a dumb quextion, but why do you to store the next possible race number in a seperate table?
    Wouldn't it be much easier to create a Query that tells you what the next race number should be?

    The following SQl pasted into a Query in Access should give you this answer, without having to store the value in a seeperate table.
    Code:
    SELECT Max([tblNumberGen]![RaceNumber])+1 AS NextRace
    FROM tblNumberGen
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Updating tables?

    Well, there is another table holding what the next number should be. I have to get the original number from tblNumberGen and then put it into the "Next" field on tblNumberLog. The "Next" field in tblNumberLog holds the number to be used next in a numbering sequence.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Updating tables?

    Why do you need to physically store this value when you can always derive it programatically using the SQL statement above?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Updating tables?

    Would I just create another query or can I stick it in another one? Im just doing what I got told to do, but if this is easier, then I guess I will try this.

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