add to a dynamic list

L

L Scholes

I have a ComboBox (txtSignature) on a userform that gets it's
information from a dynamic list (DealerName). If I enter a name that is
not in DealerName, I get an error message "Run-time error '381': Could
not get the List property. Invalid property array index." Instead of
this message, I would like to give the user a message allowing three
options:
1) add the new name to the dynamic list and enter the new name onto the
form as if the name were there all along;
2) add as if it were in the list, but don't add it to the list;
3) cancel and do nothing but clear the user form.
Using vbaMsgBoxYesNoCancel, I just don't know how to code it where
above option 1 = yes, 2 = no, 3 = cancel in the MsgBox
Does anybody have the code for this?
Thanks in advance
 
G

Guest

You can test the user response to the MsgBox as follows:

Dim vAns As Variant

vAns = MsgBox("Do you want to add it to the list?", vbYesNoCancel)

Select Case vAns
Case Is = vbYes
'add it to the list...
Case Is = vbNo
'do something as if it was added to the list...
Case is = vbCancel
'do something else...
End Select

HTH
Garry
 
L

L Scholes

As someone else on here likes to say "ask a specific question, get a
specific answer." I should have been more specific: when I attempt to
enter a name not already in the list I get a "debugger message" that
highlights the following code:

ws.Cells(iRow, 2).Value = Me.cboDealer.List(lDealer, 1)

The code you provided looks like what I need, but how do I enter it
relative to my code? Will I need an "On Error" command?
Thank you very much.
 
L

L Scholes

As someone else on here likes to say "ask a specific question, get a
specific answer." I should have been more specific: when I attempt to
enter a name not already in the list I get a "debugger message" that
highlights the following code:

ws.Cells(iRow, 2).Value = Me.cboDealer.List(lDealer, 1)

The code you provided looks like what I need, but how do I enter it
relative to my code? Will I need an "On Error" command?
Thank you very much.
 
G

Guest

Assuming cboDealer is a ComboBox control, try

ws.Cells(iRow, 2).Value = Me.cboDealer.Value

Regards,
GS
 
G

Guest

Avoid the error. Before trying to access the list, check the listindex and
see if it is valid.

with somecombobox
if .listindex = -1 and .value <> "" then
' user has entered a value not in the list
else
v = .list(.listindex)
end if
End with

If you want to trap an error, you can use
On Error Resume Next
' code that may cause an error
if err.Number <> 0 then
' an error has occured
Err.clear
else
' an error has not occured
end if
On Error goto 0

The reason you are not getting a pat answer is that a Combobox has several
settings involved here and most programming would be specific to what event,
when it is happening, how the userform is designed, what you are doing/trying
to do/ the user is doing/trying to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top