View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Stephen Bullen[_4_] Stephen Bullen[_4_] is offline
external usenet poster
 
Posts: 205
Default Helpful userform tricky to program

Hi CB,

So, in short, I need to be able to pass my City/State/Zip userform three
variables that dictate where the user wants to send the City, the State and
the Zip once they've found them. It has to be able to send to either cells
or text fields on a userform.


Don't do it like that. Instead, add those as properties of the form and
instead of closing it, hide it:

Dim mbOK As Boolean 'Whether the user OK'd or Cancelled

Private Sub cmdOK_Click()
mbOK = True
Me.Hide
End Sub

Private Sub cmdCancel_Click()
mbOK = False
Me.Hide
End Sub

'Using the [x] to close the form is the same as Cancel
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
cmdCancel_Click
Cancel = True
End If
End Sub

'Return the form's selections
Public Property Get State() As String
State = cboState.Text
End Sub

Public Property Get City() As String
City = cboCity.Text
End Sub

Public Property Get ZIP() As String
ZIP = cboZIP.Text
End Sub


Then in the calling code, you show the form, read the values and put them
wherever you want to - i.e. the calling code decides what to do with the
results:

Private Sub cmdChooseZIP_Click()

Dim oZipForm As frmZIPForm

Set oZipForm = New frmZipForm

oZipForm.Show

If oZipForm.OK Then
Range("A1").Value = oZipForm.ZIP
End If

End Sub



Regards

Stephen Bullen
Microsoft MVP - Excel

Professional Excel Development
The most advanced Excel VBA book available
www.oaltd.co.uk/ProExcelDev