Results 1 to 13 of 13
  1. #1
    thanosgr is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    56

    Add an item to combo box with vba

    Hi i've created a simple form with a combo box, using wizard, and i create this code. for to add items when isnotinlist event.
    after requery, old data are changed to primary id numbers/.

    Eg

    Nametxt, id=1
    Surnamext, id=2
    Agetxt id =3

    When i add a new item
    the first item nametxt, converts to 1. This is the id

    here is the code

    Dim cnn1 As ADODB.Connection
    Set cnn1 = CurrentProject.Connection
    Dim rs As New ADODB.Recordset

    rs.ActiveConnection = cnn1

    rs.Open " SELECT * from affectiontbl ", cnn1, adOpenDynamic, adLockPessimistic


    Dim strMsg As String


    strMsg = "'" & NewData & "' msg " & vbCrLf & vbCrLf
    strMsg = strMsg & "msg;"
    strMsg = strMsg & vbCrLf & vbCrLf & "msg"

    If MsgBox(strMsg, vbQuestion + vbYesNo, "msg;") = vbNo Then
    Response = acDataErrContinue
    Else

    rs.AddNew
    rs!data = NewData
    rs.Update
    Response = acDataErrAdded
    End If

    rs.Close
    Set rs = Nothing
    Set cnn1 = Nothing

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm going to start by suggesting you not use Reserved Words for names of any of your objects like fields. http://www.allenbrowne.com/AppIssueBadWord.html#D
    Could you also post the SQL for your RowSource?

  3. #3
    thanosgr is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    56
    SELECT [AffectionTbl].[AffectionId], [AffectionTbl].[data] FROM AffectionTbl;

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Great! Now what values do you have for:
    1) Column Count -
    2) Column Widths -
    3) List Width -
    4) Bound Column -

  5. #5
    thanosgr is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    56
    Here you are...


    1) 2
    2) 0cm.;7,434cm
    3) 7,434cm.
    4) 1

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is the [AffectionId] an AutoNumber?

  7. #7
    thanosgr is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    56
    yes its autonumber.
    when i insert a new record, it adds the record. The new records has a new id, and then updates the remaining record the first one, with the new id of the new record

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by thanosgr View Post
    . The new records has a new id, and then updates the remaining record the first one, with the new id of the new record
    Could you say that another way please. I'm uncertain what you mean.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I think it is time to *import* everything into a fresh new db and see if the problem follows: http://www.btabdevelopment.com/ts/impnew

  10. #10
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Code:
    rs.AddNew
    rs!data = NewData
    rs.Update
    I'm not sure but this block MIGHT be part of the problem. After you Add the New Record, you don't move to that empty Record before updating the Fields. If you change this to a With block, it may solve your problem:

    Code:
    With rs
        .AddNew
        !data = NewData
        .Update
    End With
    This will make sure you are adding data to the new Record and not any Record that already exists.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by Rawb View Post
    Code:
    rs.AddNew
    rs!data = NewData
    rs.Update
    I'm not sure but this block MIGHT be part of the problem. After you Add the New Record, you don't move to that empty Record before updating the Fields. If you change this to a With block, it may solve your problem:

    Code:
    With rs
        .AddNew
        !data = NewData
        .Update
    End With
    This will make sure you are adding data to the new Record and not any Record that already exists.
    I believe those two pieces of code generate the exact same execution code.

  12. #12
    thanosgr is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    56
    Hi, i follow your advice, i create a new database... with a simple form and a combo box, i copied the code as i wrote it.. and it works...

    hmmmm what happened?

    But thanks anyway

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Corruption can do strange things. http://allenbrowne.com/ser-25.html

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

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2012, 05:21 PM
  2. Missing Item List in a Combo Box
    By Robbyp2001 in forum Forms
    Replies: 19
    Last Post: 12-08-2011, 08:08 AM
  3. Combo box - Open at last item selected
    By mitchy1111 in forum Programming
    Replies: 2
    Last Post: 10-07-2011, 07:23 AM
  4. Replies: 7
    Last Post: 08-31-2011, 05:09 PM
  5. Combo-box to select item to edit in Form
    By DHavokD in forum Forms
    Replies: 7
    Last Post: 06-05-2009, 01:39 PM

Tags for this Thread

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