Access 2017 - Manejo de Columnas
Access 2017 - Manejo de Columnas
By Helen Feddema
Access versions: 2002-2010
Level: Intermediate
Introduction
If you let users enter country names into a field on a form, you may end up with several
variations on a single country name, such as UK, United Kingdom, England, Britain.
This leads to problems when getting totals or filtering by country. To avoid this, you can
prepare a lookup table of country names. That works fine for a limited selection (say,
just NAFTA countries or EuroZone countries). But what if a customer could be in any
country in the world? Users will balk at making a selection from a list of 100 or so
countries. This article describes a way of making country selection both easy and
accurate.
strDescription = Nz(Me![cboDescription].Value)
Set txt = Me![txtIDOrPhone]
Set cbo = Parent![cboContactCountry]
strInternetCode = Nz(cbo.Column(1))
strCountryCode = Nz(cbo.Column(2))
strCity = Nz(Parent![txtContactCity])
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
intLength = Len(strRawPhoneNo)
strNumberPortion = Left(strRawPhoneNo, 10)
Case "US"
strTitle = "Format problem"
strPrompt = "United States phone numbers must have 10 digits"
Case 10
FormattedPhoneNo = Format(strRawPhoneNo, "(###) ###-####")
Case Is < 10
MsgBox strPrompt, vbExclamation, strTitle
Case Is > 10
If intxPosition > 0 Then
FormattedPhoneNo = Format(strNumberPortion, _
"(###) ###-####") _
& " " & Trim(strExtPortion)
Else
MsgBox strPrompt, vbExclamation, strTitle
End If
End Select
Case "CA"
strTitle = "Format problem"
strPrompt = "Canadian phone numbers must have 10 digits"
Case 10
FormattedPhoneNo = Format(strRawPhoneNo, "(###) ###-####")
Case Is > 10
If intxPosition > 0 Then
FormattedPhoneNo = Format(strNumberPortion, _
"(###) ###-####") _
& " " & Trim(strExtPortion)
Else
MsgBox strPrompt, vbExclamation, strTitle
End If
End Select
Case "UK"
Case "AU"
strTitle = "Format problem"
strPrompt = "Australian phone numbers must have 10 digits"
Case 10
If blnCell = True Then
FormattedPhoneNo = "+" & strCountryCode & " " _
& Format(strRawPhoneNo, "####-###-###")
ElseIf blnCell = False Then
FormattedPhoneNo = "+" & strCountryCode & " " _
& Format(strRawPhoneNo, "(##) ####-####")
End If
Case Is < 10
MsgBox strPrompt, vbExclamation, strTitle
Case Is > 10
If intxPosition > 0 Then
Case "ZA"
strTitle = "Format problem"
strPrompt = "South African phone numbers must have 10 digits"
Case 10
FormattedPhoneNo = Format(strRawPhoneNo, "(###) ###-####")
Case Is < 10
MsgBox strPrompt, vbExclamation, strTitle
Case Is > 10
If intxPosition > 0 Then
FormattedPhoneNo = Format(strNumberPortion, _
"(###) ###-####") & " " & Trim(strExtPortion)
Else
MsgBox strPrompt, vbExclamation, strTitle
End If
End Select
Case "NZ"
strTitle = "Format problem"
strPrompt = "New Zealand phone numbers must have from 9 to 11 digits"
Case 9
If blnCell = False Then
FormattedPhoneNo = "+" & strCountryCode & " " _
& Format(strRawPhoneNo, "(##) ###-####")
Case 10
If blnCell = True Then
FormattedPhoneNo = "+" & strCountryCode & " " _
& Format(strRawPhoneNo, "(###) ###-####")
ElseIf blnCell = False Then
MsgBox strPrompt, vbExclamation, strTitle
End If
Case 11
If blnCell = True Then
FormattedPhoneNo = "+" & strCountryCode & " " _
& Format(strRawPhoneNo, "(####) ###-####")
ElseIf blnCell = False Then
MsgBox strPrompt, vbExclamation, strTitle
End If
Case Else
MsgBox strPrompt, vbExclamation, strTitle
End Select
End Select
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in FormattedPhoneNo procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Function
intLength = Len(strRawPhoneNo)
strNumberPortion = Left(strRawPhoneNo, 10)
intxPosition = Nz(InStr(strRawPhoneNo, "x"))
Case 10
FormattedUSCanadaPhoneNo = Format(strRawPhoneNo, _
"(###) ###-####")
Case Is < 10
MsgBox strPrompt, vbExclamation, strTitle
Case Is > 10
If intxPosition > 0 Then
FormattedUSCanadaPhoneNo = Format(strNumberPortion, _
"(###) ###-####") & " " & Trim(strExtPortion)
Else
MsgBox strPrompt, vbExclamation, strTitle
End If
End Select
ErrorHandlerExit:
Exit Function
ErrorHandler:
End Function
For i = 1 To Len(strStripChars)
strBadChar = Mid(strStripChars, i, 1)
strTestString = Replace(strTestString, strBadChar, vbNullString)
Next
StripNonAlphaNumericChars = strTestString
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in StripNonAlphaNumericChars procedure; " _
& "Description: " & Err.Description
End Function
VBA Code
The frmCountries code module is listed below (frmTimeZones has similar code):
Option Explicit
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
lstSelected.Requery
lstAvailable.Requery
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
lstSelected.Requery
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
lstSelected.Requery
lstSelected.RowSource = strQuery
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
lstSelected.RowSource = strQuery
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
strTable = "tblCountries"
strCountry = Nz(lstAvailable.Column(0))
lstSelected.Requery
lstAvailable.Requery
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.ActiveControl.Name & " procedure; " _
& "Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err.Number = 40036 Then
Resume ErrorHandlerExit
Else
MsgBox "Error No: " & Err.Number _
& " in Form_Close procedure; Description: " _
& Err.Description
Resume ErrorHandlerExit
End If
End Sub
DoCmd.RunCommand acCmdSizeToFitForm
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number _
& " in " & Me.Name & " Form_Load procedure; " _
& "Description: " & Err.Description
End Sub
References
The code in the sample database does not need any special references.
Supporting Files
The zip file containing this article, in Word 97-2003 format, plus the supporting file(s),
may be downloaded from the Access Archon page of my Web site, as accarch218.zip,
which is the last entry in the table of Access Archon columns for Access Watch.
Document Name Document Type Place in
Selecting Countries (AA Access 2002-2003 database Wherever you want
218).mdb (can also be used in higher
versions of Access)