A2003, XP Pro.
I'm writing a VBA procedure to remove wildcard characters from a number of
fields in various tables. I'm calling a sub routine and passing in strings
representing the character to be replaced, the field name and the table
name. All works fine until I try to get the data string out of the field by
referencing the string variable, at which point I get an "object variable
not set" error. I know this is going to be something daft but I can't quite
fathom it out - any ideas? Many thanks, Keith.
Code:
Sub libReplaceWildcards()
Call libWildcards("&", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine
for "&"
Call libWildcards("%", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine
for "%"
Call libWildcards("#", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine
for "#"
Call libWildcards("&", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the
routine for "&"
Call libWildcards("%", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the
routine for "%"
Call libWildcards("#", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the
routine for "#"
End Sub
Sub libWildcards(strSearchChar As String, strField As String, strTable As
String)
Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String, intPosition
As Integer
Dim strSearchString As String, strNewString As String, strReplaceChar As
String
Set db = CurrentDb
If strSearchChar = "&" Then strNewString = "AND"
If strSearchChar = "%" Then strNewString = "PERCENT"
strSQL = "Select " & strField & " From " & strTable & " Where " & strField &
" Like '*[" & strSearchChar & "]*'"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then Exit Sub
With rs
.MoveFirst
Do Until .EOF
strSearchString = rs!strField *** THIS IS WHERE THE ERROR OCCURS ***
intPosition = InStr(1, strSearchString, strSearchChar, 1) - 1 'Count
the characters before the wildcard
If strSearchChar = "#" Then 'Hashes are removed but may leave spaces
behind
strNewString = Left(strSearchString, intPosition) 'Get string
before the hash and space characters
Else
strNewString = Left(strSearchString, intPosition) _
& strReplaceChar 'Add the replacement for the wildcard
End If
strNewString = strNewString _
& Right(strSearchString, Len(strSearchString) - (intPosition + 2))
'Add the characters after the wildcard
.Edit
![strField] = strNewString
.Update
Debug.Print strNewString
.MoveNext
Loop
End With
End Sub 2 2269
"Keith" <ke*********@baeAWAYWITHITsystems.com> wrote in message
news:42**********@glkas0286.greenlnk.net... A2003, XP Pro.
I'm writing a VBA procedure to remove wildcard characters from a number of fields in various tables. I'm calling a sub routine and passing in strings representing the character to be replaced, the field name and the table name. All works fine until I try to get the data string out of the field by referencing the string variable, at which point I get an "object variable not set" error. I know this is going to be something daft but I can't quite fathom it out - any ideas? Many thanks, Keith.
Code:
Sub libReplaceWildcards()
Call libWildcards("&", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine for "&" Call libWildcards("%", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine for "%" Call libWildcards("#", "DRG_TITLE", "OMSADM_OMS_DRG_TBL") 'Call the routine for "#"
Call libWildcards("&", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the routine for "&" Call libWildcards("%", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the routine for "%" Call libWildcards("#", "VER_DRG_TITLE", "OMSADM_OMS_VERSION_TBL") 'Call the routine for "#"
End Sub
Sub libWildcards(strSearchChar As String, strField As String, strTable As String)
Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String, intPosition As Integer Dim strSearchString As String, strNewString As String, strReplaceChar As String Set db = CurrentDb
If strSearchChar = "&" Then strNewString = "AND" If strSearchChar = "%" Then strNewString = "PERCENT"
strSQL = "Select " & strField & " From " & strTable & " Where " & strField & " Like '*[" & strSearchChar & "]*'" Set rs = db.OpenRecordset(strSQL) If rs.RecordCount = 0 Then Exit Sub
With rs .MoveFirst Do Until .EOF strSearchString = rs!strField *** THIS IS WHERE THE ERROR OCCURS *** intPosition = InStr(1, strSearchString, strSearchChar, 1) - 1 'Count the characters before the wildcard
If strSearchChar = "#" Then 'Hashes are removed but may leave spaces behind strNewString = Left(strSearchString, intPosition) 'Get string before the hash and space characters Else strNewString = Left(strSearchString, intPosition) _ & strReplaceChar 'Add the replacement for the wildcard End If
strNewString = strNewString _ & Right(strSearchString, Len(strSearchString) - (intPosition + 2)) 'Add the characters after the wildcard
.Edit ![strField] = strNewString .Update
Debug.Print strNewString .MoveNext Loop End With
End Sub
rs!strField would be what you would write if your field name was literally
"strField"
rs.Fields(strField) or simply rs(strField) is what you need
Don't forget that if your field could be null, then you should use the Nz
function before assigning a a field's value to a variable of type string.
"Justin Hoffman" <j@b.com> wrote in message
news:d6**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com... rs!strField would be what you would write if your field name was literally "strField" rs.Fields(strField) or simply rs(strField) is what you need Don't forget that if your field could be null, then you should use the Nz function before assigning a a field's value to a variable of type string.
That's it! It's so darned obvious now you've shown me, many thanks Justin
:o)
Regards,
Keith. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bharath Dhurjati |
last post by:
Hello,
I am looking for documentation that specifies the following behavior
exhibited by java.
The following (assuming MyClass.class is accessible and has a main())
java MyClass *
yields...
|
by: Ralph Noble |
last post by:
I thought this problem would go away over the Christmas holiday, but
of course it did not. I'm trying to write a stored procedure
incorporating wildcards, so I can search for variations. Example,...
|
by: Shyguy |
last post by:
I need to import a text file pretty much daily. I download the file
and change the name to a standard name and then run the code to import
the file into a table in my database. The problem is...
|
by: Alvaro Puente |
last post by:
Hi all!
Do any of you know if wildcards are accepted when calling rename()
function?
Thanks/Alvaro
|
by: Anandan |
last post by:
Hi,
This is regarding Dataset Filter:
WILDCARD CHARACTERS
Both the * and % can be used interchangeably for wildcards in a LIKE
comparison. If the string in a LIKE clause contains a * or %,...
| |
by: Alan Carpenter |
last post by:
Access 8 on Win98 and WinXP
I'm having trouble with wildcards in the .Filename property of the
FileSearch Object giving different results on win98 and XP. I've been
successfully using...
|
by: Nitinkcv |
last post by:
Hi,
I have a textbox and a button. In my textbox i have to enter the query string(say shoes) and on clicking the button takes me to a page show all item related to the search string( in this case...
|
by: Jeff |
last post by:
I tend to be a bit behind the times, sometimes a generation further
that I want.
So, I've been thing about the "*" selector. I've noticed that unlike
body, it styles form fields and form...
|
by: nidaar |
last post by:
From a security point of view, is accepting wildcards like "%" in
input parameters of stored procedures against any best practices?
As an example, if a user defined function uses "Productname...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |