Option Explicit
Private Sub Form_Load()
' Do whatever you do to set the intial
' value of the Port here from your DataSource
' And make sure it's selected.
cboPort.AddItem "21"
cboPort.ListIndex = 0
End Sub
Private Sub cboPort_DropDown()
Static bPortsLoaded As Boolean
' If the Ports haven't been loaded yet
' Load them Now.
If Not bPortsLoaded Then LoadPorts
' Only Load the Ports once, so track
' That we've done it in a Static variable.
bPortsLoaded = True
End Sub
Private Sub LoadPorts()
Dim iFile As Integer
Dim sPorts As String
Dim vPort As Variant
Dim lPort As Long, lIndex As Long
' Open the Port File
iFile = FreeFile
Open "C:\Ports.txt" For Binary Access Read As iFile
sPorts = Space(LOF(iFile))
Get #iFile, , sPorts
Close iFile
' Split it into Port Entries
vPort = Split(sPorts, vbCrLf)
' Remember the current Port
sPorts = cboPort.Text
' Clear the Combo
cboPort.Clear
' Populate the Combo with ALL the ports.
For lPort = 0 To UBound(vPort)
cboPort.AddItem vPort(lPort)
' If this is the port that was selected
' Remember the ListIndex, so it's selected in
' The DropList.
If vPort(lPort) = sPorts Then lIndex = lPort
Next
' Set the ListIndex to the Original Port
' So it's Selected when the list is Dropped.
cboPort.ListIndex = lIndex
End Sub