Solved

How to rename or add as new vb 6 combo box

Posted on 2014-10-02
12
85 Views
Last Modified: 2014-10-06
Form level variable
Dim mstrCboText As String
Combo Style 0 Dropdown combo

Private Sub cboBids_Click()
If Me.cboBids.ListIndex <> -1 Then
  mstrCboText = cboBids.Text' put the text into variable
End If
End Sub

Private Sub cboBids_LostFocus()

If Me.cboBids.ListIndex <> -1 Then
  mstrCboText <> cboBids.Text' 


End If 

If Me.cboBids.ListIndex <> -1 Then
     If mstrCboText <> cboBids.Text Then
             Select Case MsgBox("Do you want to rename mstrCboText to cboBids.Text click ""Yes""" _
                                & vbCrLf & "To add as as new Catagory click ""No""" _
                                & vbCrLf & "To cancel click""Cancel""" _
                                , vbYesNoCancel Or vbQuestion Or vbDefaultButton1, App.Title)
             
                Case vbYes
             
                Case vbNo
             
                Case vbCancel
                   cboBids.Text = mstrCboText ' put back the original text
             End Select 

End If

Open in new window


How to do the vbyes(rename) and make it selected ?
How to do the vbNo (Add as a new item and make it selected ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40357075
Can you try again to explain what it is that you want to do? Are you just trying to change an item in the combobox?
0
 

Author Comment

by:isnoend2001
ID: 40357306
An existing item is selected and then typed over I am trying to either replace or add a new item and make it selected
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40357491
Option Explicit

Dim mstrCboText As String
Private mintLI As Integer
Private Sub Form_Load()
cboBids.AddItem "This"
cboBids.AddItem "is"
cboBids.AddItem "a"
cboBids.AddItem "test"
End Sub


Private Sub cboBids_Click()
If Me.cboBids.ListIndex <> -1 Then
    mintLI = cboBids.ListIndex
    mstrCboText = cboBids.Text ' put the text into variable
End If
End Sub

Private Sub cboBids_LostFocus()

If Me.cboBids.ListIndex <> -1 Then
  'mstrCboText <> cboBids.Text


End If


If mstrCboText <> "" Then
     If mstrCboText <> cboBids.Text Then
             Select Case MsgBox("Do you want to rename '" & mstrCboText & "' to '" & cboBids.Text & "'? " _
                                & vbCrLf & vbCrLf _
                                & "Click ""Yes"" to rename the entry" & vbCrLf _
                                & "Click ""No"" to add as a new Catagory " & vbCrLf _
                                & "Click""Cancel"" to ignore the change" _
                                , vbYesNoCancel Or vbQuestion Or vbDefaultButton1, App.Title)
             
                Case vbYes
                    cboBids.List(mintLI) = cboBids.Text
                    cboBids.ListIndex = mintLI
                Case vbNo
                    cboBids.AddItem cboBids.Text
                Case vbCancel
                   cboBids.Text = mstrCboText ' put back the original text
             End Select
    End If
End If
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:isnoend2001
ID: 40357589
thanks
never realized it that after selecting an item then, typing over the name changes the listindex to -1
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40357724
To be honest I didn't either until I tried it. I also didn't realize that I posted my test data:) In any case I'm glad I could help.
0
 

Author Comment

by:isnoend2001
ID: 40358440
Hi martin Liss
After I use the variable mintLI
How do i reset it?
0  could be a selection
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40358447
You don't need to reset it since it is always set to the ListIndex of the selected entry. And I don't know if you are concerned about 0 - that only means that the first item was clicked.
0
 

Author Comment

by:isnoend2001
ID: 40358486
Thanks had a thought process going where i needed to check it's value. Don't remember what it was
I think it had something to do with  differentiate, between renaming and just adding as new
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40358487
If you notice, while it's always set, it's only used when renaming.
0
 

Author Comment

by:isnoend2001
ID: 40365101
Hi MartinLiss
Finally am getting to using this. In looking at the code it does not look as though
adding as new the new item is not selected. I think adding items puts them at the bottom of the list,
maybe cboBids.listindex = cboBids.listcount - 1
I will try
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40365106
By default new items are added at the bottom of the combobox list. You can change that behavior in two ways.

1) Set the Sorted property of the combobox to True, or
2) Use code like this Combo1.AddItem "bbb", 1 which would add "bbb" as the second item in the list.
0
 

Author Comment

by:isnoend2001
ID: 40365126
thanks martinLiss
Adding to bottom of the combobox list is ok if this will work
cboBids.listindex = cboBids.listcount - 1
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Contents of a Directory In Access VBA 5 89
How does CurrentUser work? 10 42
VBA Shell can't Find Word document 11 131
Passing a Text Box name to a Sub 6 102
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question