PDA

View Full Version : If Then Else / If Trim (help :))



nickjones87
05-04-2009, 03:57 PM
If Trim(Me.Country.Value) = "United Kingdom" Or "Austria" Or "Belgium" Or "Bulgaria" Or "Cyprus" Or "Czech Republic" _
Or "Denmark" Or "Estonia" Or "Finland" Or "France" Or "Germany" Or "Greece" Or "Hungary" Or "Ireland" Or "Italy" _
Or "Latvia" Or "Lithuania" Or "Luxembourg" Or "Malta" Or "Netherlands" Or "Poland" Or "Portugal" Or "Romania" _
Or "Slovakia" Or "Slovenia" Or "Spain" Or "Sweden" Then
Me.cn22.Visible = False

Else
Me.cn22.Visible = True
End If
------------
Can anybody tell me what is wrong with this code?
runtime error 13 type mismatch

Thanks In Advance...
Nick:banghead:

hansup
05-04-2009, 11:21 PM
If Trim(Me.Country.Value) = "United Kingdom" Or "Austria" Or "Belgium" Or "Bulgaria" Or "Cyprus" Or "Czech Republic" Or "Denmark" Or "Estonia" Or "Finland" Or "France" Or "Germany" Or "Greece" Or "Hungary" Or "Ireland" Or "Italy" Or "Latvia" Or "Lithuania" Or "Luxembourg" Or "Malta" Or "Netherlands" Or "Poland" Or "Portugal" Or "Romania" Or "Slovakia" Or "Slovenia" Or "Spain" Or "Sweden" Then
Me.cn22.Visible = False

Else
Me.cn22.Visible = True
End If You would have to rewrite it as:
If Trim(Me.Country.Value) = "United Kingdom" Or _
Trim(Me.Country.Value) = "Austria" Or _
...

That approach will get ugly fast. If you have the country names in a table which includes an attribute to distinguish them, you could use DLookup with Me.Country.Value instead of that long "Or" list.

I tried another approach using a Dictionary to hold the country names.

Option Compare Database
Option Explicit
Private mobjDict As Object

Private Sub cmdCheckCountry_Click()
If countryInDict(Nz(Me.txtCountry.Value, "")) = True Then
MsgBox "Country found in dictionary"
Else
MsgBox "Country not found in dictionary"
End If

End Sub

Private Function countryInDict(strIn As String) As Boolean
If mobjDict Is Nothing Then
Set mobjDict = CreateObject("Scripting.Dictionary")
mobjDict.Add "United Kingdom", 1
mobjDict.Add "Austria", 1
mobjDict.Add "Belgium", 1
mobjDict.Add "Bulgaria", 1
mobjDict.Add "Cyprus", 1
mobjDict.Add "Czech Republic", 1
mobjDict.Add "Denmark", 1
mobjDict.Add "Estonia", 1
mobjDict.Add "Finland", 1
mobjDict.Add "France", 1
mobjDict.Add "Germany", 1
mobjDict.Add "Greece", 1
mobjDict.Add "Hungary", 1
mobjDict.Add "Ireland", 1
mobjDict.Add "Italy", 1
mobjDict.Add "Latvia", 1
mobjDict.Add "Lithuania", 1
mobjDict.Add "Luxembourg", 1
mobjDict.Add "Malta", 1
mobjDict.Add "Netherlands", 1
mobjDict.Add "Poland", 1
mobjDict.Add "Portugal", 1
mobjDict.Add "Romania", 1
mobjDict.Add "Slovakia", 1
mobjDict.Add "Slovenia", 1
mobjDict.Add "Spain", 1
mobjDict.Add "Sweden", 1
End If

If mobjDict.Exists(strIn) Then
countryInDict = True
Else
countryInDict = False
End If

End Function

Private Sub Form_Unload(Cancel As Integer)
Set mobjDict = Nothing
End Sub

OBP
05-05-2009, 02:38 AM
It might be easier to use the coutries that you want the field to be visible for if there are less of them.