Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91

    VB and SQL confusion

    I am trying to create a value that will be used in a form. It is generated from a table (CaseNumber) which has only one record in it. I need to create the value then append it to the table and form. Then delete the origional record...

    Private Sub Command11_Click()
    Dim intIncrementalNumber As Integer
    intIncrementalNumber = IIf(IsNull(DMax("[CNumber]", "CaseNumber")), 0, DMax("[CNumber]", "CaseNumber"))
    Me.[CRNumber] = "C" & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1
    DoCmd.RunSQL "INSERT INTO CaseNumber( CNumber )VALUE (intIncrementalNumber +1);"



    Can I use a variable from VB in an SQL statement? (intIncrementalNumber)

    Thanks
    Wayne

  2. #2
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    Sorry I forgot to mention....How do I get rid of the message box that will come up asking the user if they want to append the table with VB?

    Thanks
    Wayne

  3. #3
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    change:
    Code:
    DoCmd.RunSQL "INSERT INTO CaseNumber( CNumber )VALUE (intIncrementalNumber +1);"
    to:
    Code:
    DoCmd.RunSQL "INSERT INTO CaseNumber( CNumber ) VALUES (" & intIncrementalNumber +1 & ");"
    and to suppress the message, change:
    Code:
    DoCmd.RunSQL
    to:
    Code:
    currentdb.execute

  4. #4
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    Thanks that was helpful...until I tried it with this line: CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES ('C' " & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1 & ");"

    The error is that it is missing an operator?? [CRNumber] is a text field. Any ideas?

    Wayne

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Wayne311 View Post
    Thanks that was helpful...until I tried it with this line: CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES ('C' " & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1 & ");"

    The error is that it is missing an operator?? [CRNumber] is a text field. Any ideas?

    Wayne
    Wayne,

    I'm not sure from looking at this what you want, but do what the pros do. Put that entire statement inside of a string variable and print it out in the imm. window. look at it, then you'll know if it's right or not. so do:
    Code:
    var = "INSERT INTO CaseNumber ( CRNumber ) VALUES ('C' " & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1 & ");"
    
    debug.print var
    
    'break point on next line to check printout

  6. #6
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    I think I am getting bleary eyed on this...second day trying to insert a record into a table (CaseNumber) and the same field in a form.

    intIncrementalNumber = IIf(IsNull(DMax("[CNumber]", "CaseNumber")), 0, DMax("[CNumber]", "CaseNumber"))

    For the form I use this: Me.[CRNumber] = "C" & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1

    It creates C11-123. But I cannot get the same to work in another one-record table that is used to create the string (C11-123). Both of these fields are text. For the table (CaseNumber) I am trying this: CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES ('C' " & Right(Year(Date), 2) & intIncrementalNumber + 1 & ");"

    I have broken it into pieces and I can get the "C11" but not any further.

    Wayne

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Wayne311 View Post
    I think I am getting bleary eyed on this...second day trying to insert a record into a table (CaseNumber) and the same field in a form.

    intIncrementalNumber = IIf(IsNull(DMax("[CNumber]", "CaseNumber")), 0, DMax("[CNumber]", "CaseNumber"))

    For the form I use this: Me.[CRNumber] = "C" & Right(Year(Date), 2) & "-" & intIncrementalNumber + 1

    It creates C11-123. But I cannot get the same to work in another one-record table that is used to create the string (C11-123). Both of these fields are text. For the table (CaseNumber) I am trying this: CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES ('C' " & Right(Year(Date), 2) & intIncrementalNumber + 1 & ");"

    I have broken it into pieces and I can get the "C11" but not any further.

    Wayne
    I really don't know what you're talking about, Wayne. Your description is very confusing to me. Did you even try to implement the printing technique that I told you about in my last thread?? Seriously, that is what you need to do. if and WHEN you do print it, do you know what you're looking for??

  8. #8
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    Attached is the error I am getting.

    Wayne

  9. #9
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    I am not sure what it was supposed to do...It did nothing apparant.

    Sorry

  10. #10
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Wayne311 View Post
    I am not sure what it was supposed to do...It did nothing apparant.

    Sorry
    that error can mean anything and everything, Wayne. It's one of the great annoyances of Microsoft technology.

    It cannot be deciphered by anyone, because in all reality, no pinpointing can be done from it. sorry.

    Someone might be able to take a look for you if you upload what you got, what you want, and point people to the EXACT place to fix it for you, so it doesn't take too much time. personally, that's the only way I would do it, because I'm stetched for time as it is.

  11. #11
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    How would I insert a variable (string)?

    INSERT INTO CaseNumber ( CRNumber ) VALUES var

    Wayne

  12. #12
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Wayne311 View Post
    INSERT INTO CaseNumber ( CRNumber ) VALUES var
    if 'var' is a string, do this:
    Code:
    "INSERT INTO CaseNumber ( CRNumber ) VALUES '" & var & "'"
    I don't use VALUES often, so it may need parenthesis too.

    remember though, if that VAR has quotes stored inside of it, what I just said means nothing. quotes are the single most annoying entity in access, and visual basic for that matter, according to me.

  13. #13
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    I tried it with double and single quotes (with both and with just each) and get a syntax error...

    CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES '" & strVar & "'

  14. #14
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Wayne311 View Post
    I tried it with double and single quotes (with both and with just each) and get a syntax error...

    CurrentDb.Execute "INSERT INTO CaseNumber ( CRNumber ) VALUES '" & strVar & "'
    upload your stuff and tell us what you're needing.

  15. #15
    Wayne311 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    91
    Attached is my db. With the RcptData form, the "New Case #" button creates a case number (C11-123) and places it in the form. I want it also to put it in table (CaseNumber, CRNumber) as it did in table (RcptCRDistrict).

    Thanks
    Wayne

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Events Confusion?
    By homerj56 in forum Programming
    Replies: 5
    Last Post: 10-27-2010, 01:45 PM
  2. Combo box lookup confusion
    By redpenner in forum Forms
    Replies: 5
    Last Post: 08-19-2010, 08:45 PM
  3. Confusion
    By colenzo in forum Access
    Replies: 4
    Last Post: 07-21-2009, 06:38 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