473,503 Members | 12,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search & Replace Function

132 New Member
I'm trying to create a search & replace function in Access. Specifically, I want to search for a specific SSN and replace it, if found. Not sure how to code the "guts" of it. Here is what I have so far:

Private Sub CmdUpdateAppeal_Click()
SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
If SSNreplace = "" Then

MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"

End
End If

If Not SSNreplace


If MsgBox("Is this the correct client?", vbYesNoCancel) <> vbYes Then
GoTo Flag1
End If
Else
MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"

End If

End Sub

The field name is called PersonID
Jul 12 '07 #1
16 3007
puppydogbuddy
1,923 Recognized Expert Top Contributor
I'm trying to create a search & replace function in Access. Specifically, I want to search for a specific SSN and replace it, if found. Not sure how to code the "guts" of it. Here is what I have so far:

Private Sub CmdUpdateAppeal_Click()
SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
If SSNreplace = "" Then

MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"

End
End If

If Not SSNreplace


If MsgBox("Is this the correct client?", vbYesNoCancel) <> vbYes Then
GoTo Flag1
End If
Else
MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"

End If

End Sub

The field name is called PersonID
You did not elaborate much on the replace function, but the following link contains an excellent step by step and code for creating a search facility.

http://www.databasedev.co.uk/text_search.html
Jul 12 '07 #2
Proaccesspro
132 New Member
You did not elaborate much on the replace function, but the following link contains an excellent step by step and code for creating a search facility.

http://www.databasedev.co.uk/text_search.html

Thanks. I can relate to the search part of it, it's the replace part I'm not sure about....Basically, what I will want to do is update the corressponding SSN, if found, with the a corrected version....in other words, the user will enter a SSN into the input box, if found, the user will then have the opportunity to input a new SSN that will over-write the intial one.
Jul 12 '07 #3
puppydogbuddy
1,923 Recognized Expert Top Contributor
Thanks. I can relate to the search part of it, it's the replace part I'm not sure about....Basically, what I will want to do is update the corressponding SSN, if found, with the a corrected version....in other words, the user will enter a SSN into the input box, if found, the user will then have the opportunity to input a new SSN that will over-write the intial one.
Ok, what you are describing is not a replace function; you want to "clear" the search control (your input box) for the next entry. This is done by setting focus on the bound control PersonID and setting the input box to an empty string (“”). The code is contained in the link I gave you. I have excerpted the portion of the code that clears the input box (see below) and tried to make it more relevant by using object names similar to the ones you are using. All you need to do is substitute the actual name of your objects
Expand|Select|Wrap|Line Numbers
  1. ‘PersonID is the control bound to table (strStudentRef in the link).
  2. ‘strSearchID is the variable that captures the search string that was entered into the textbox (strSearch in the link)
  3. ‘txtSearchID is the textbox used to enter the search string (txtSearch in the link) 
  4. 'If matching record found, sets focus in PersonID, shows msgbox
  5. 'and clears search control
  6.  
  7.     If PersonID = strSearchID Then
  8.         MsgBox "Match Found For: " & strSearchID, , "Search Result"
  9.         PersonID.SetFocus
  10.         txtSearchID = ""                                     ‘textbox equivalent to your input box
  11.  
  12.  
  13.   End If
Jul 13 '07 #4
Proaccesspro
132 New Member
Ok, what you are describing is not a replace function; you want to "clear" the search control (your input box) for the next entry. This is done by setting focus on the bound control PersonID and setting the input box to an empty string (“”). The code is contained in the link I gave you. I have excerpted the portion of the code that clears the input box (see below) and tried to make it more relevant by using object names similar to the ones you are using. All you need to do is substitute the actual name of your objects
Expand|Select|Wrap|Line Numbers
  1. ‘PersonID is the control bound to table (strStudentRef in the link).
  2. ‘strSearchID is the variable that captures the search string that was entered into the textbox (strSearch in the link)
  3. ‘txtSearchID is the textbox used to enter the search string (txtSearch in the link) 
  4. 'If matching record found, sets focus in PersonID, shows msgbox
  5. 'and clears search control
  6.  
  7.     If PersonID = strSearchID Then
  8.         MsgBox "Match Found For: " & strSearchID, , "Search Result"
  9.         PersonID.SetFocus
  10.         txtSearchID = ""                                     ‘textbox equivalent to your input box
  11.  
  12.  
  13.   End If

Not quite getting the results I thought...I tested the code, even when I enter a REAL SSN, it will NOT find a match ...Also, is there a way to format the input box with an input mask of xxx-xx-xxxx?? Here is the code I have:

Dim SSNreplace As String

SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")

If SSNreplace = "" Then

MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"

GoTo Flag1

End If


If SSNreplace = SSN Then
MsgBox "Match Found For: " & SSNreplace, vbInformation, "Update a SSN"
PersonID.SetFocus
txtSearchID = "" 'textbox equivalent to your input box

Else

MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"

End If


Flag1:

End Sub
Jul 16 '07 #5
puppydogbuddy
1,923 Recognized Expert Top Contributor
Not quite getting the results I thought...I tested the code, even when I enter a REAL SSN, it will NOT find a match ...Also, is there a way to format the input box with an input mask of xxx-xx-xxxx?? Here is the code I have:

Dim SSNreplace As String

SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")

If SSNreplace = "" Then

MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"

GoTo Flag1

End If


If SSNreplace = SSN Then
MsgBox "Match Found For: " & SSNreplace, vbInformation, "Update a SSN"
PersonID.SetFocus
txtSearchID = "" 'textbox equivalent to your input box

Else

MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"

End If


Flag1:

End Sub
I will look at your code tomorrow. In the meantime, here is my response to your questions re: input mask for input box:

The inputbox doesn't have an input mask feature. You would be better off incorporating the mask in a a textbox control on the search form (like the one in the link I gave you), or on a small popup form by itself. Here’s the steps involved in creating your own form:

1. Create an unbound form with an unbound textbox control.
2. Set the control's input mask property.
3. Add a command button to this form.
4. Code it's click event:
Me.Visible = False
Name this form "SSNMaskForm"

You would then call this form (instead of the InputBox).

DoCmd.OpenForm "SSNMaskForm", , , , , acDialog
If Forms!FormName!ControlName = "SSN" then
' Correct SSN do whatever is wanted here
Else
' Wrong SSN msgbox.
End if
DoCmd.Close acForm, "SSNMaskForm"
Jul 16 '07 #6
Proaccesspro
132 New Member
I will look at your code tomorrow. In the meantime, here is my response to your questions re: input mask for input box:

The inputbox doesn't have an input mask feature. You would be better off incorporating the mask in a a textbox control on the search form (like the one in the link I gave you), or on a small popup form by itself. Here’s the steps involved in creating your own form:

1. Create an unbound form with an unbound textbox control.
2. Set the control's input mask property.
3. Add a command button to this form.
4. Code it's click event:
Me.Visible = False
Name this form "SSNMaskForm"

You would then call this form (instead of the InputBox).

DoCmd.OpenForm "SSNMaskForm", , , , , acDialog
If Forms!FormName!ControlName = "SSN" then
' Correct SSN do whatever is wanted here
Else
' Wrong SSN msgbox.
End if
DoCmd.Close acForm, "SSNMaskForm"
OK....I'll wait for your reply..just to be clear...my intent is to allow the user to input a SSN, use the nyumber entered to search the databse for a match, if found it would allow the user via an additional input box to enter a corrected SSN. Once enterd, a message box would indicate the update was successful. I'm thinking that is why I want the input mask so they will be forced to enter the correct number of digits. Thanks for the help!!
Jul 16 '07 #7
puppydogbuddy
1,923 Recognized Expert Top Contributor
OK....I'll wait for your reply..just to be clear...my intent is to allow the user to input a SSN, use the nyumber entered to search the databse for a match, if found it would allow the user via an additional input box to enter a corrected SSN. Once enterd, a message box would indicate the update was successful. I'm thinking that is why I want the input mask so they will be forced to enter the correct number of digits. Thanks for the help!!
Try this revised code. You have my comments about the input box.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FindSSN_Click()
  2. On Error GoTo Flag2
  3.  
  4. Dim SSNreplace As String
  5.  
  6. StartHere:
  7.  
  8. DoCmd.GoToControl "SSN"           ‘Is your control named SSN or PersonID?
  9. SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
  10. DoCmd.FindRecord SSNreplace, acAnywhere, , acSearchAll, , acCurrent, True
  11. If SSNreplace = "" Then
  12.     MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"
  13. ElseIf SSNreplace = SSN Then
  14.     MsgBox "Match Found For: " & SSNreplace, vbInformation, "Update a SSN"
  15.     GoTo Flag1
  16. Else
  17.     MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"
  18.     GoTo Flag1
  19. End If
  20.  
  21.  
  22. Flag1:
  23.     SSNReplace = “”    ‘reset (clear) variable for reuse
  24.     Exit Sub
  25.  
  26. Flag2:
  27.     MsgBox Err.Description
  28.     Resume Flag1:
  29.  
  30. End Sub
  31.  
Jul 17 '07 #8
Proaccesspro
132 New Member
Try this revised code. You have my comments about the input box.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FindSSN_Click()
  2. On Error GoTo Flag2
  3.  
  4. Dim SSNreplace As String
  5.  
  6. StartHere:
  7.  
  8. DoCmd.GoToControl "SSN"           ‘Is your control named SSN or PersonID?
  9. SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
  10. DoCmd.FindRecord SSNreplace, acAnywhere, , acSearchAll, , acCurrent, True
  11. If SSNreplace = "" Then
  12.     MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"
  13. ElseIf SSNreplace = SSN Then
  14.     MsgBox "Match Found For: " & SSNreplace, vbInformation, "Update a SSN"
  15.     GoTo Flag1
  16. Else
  17.     MsgBox "Sorry, SSN NOT Found. Please Try Again", vbCritical, "Update a SSN"
  18.     GoTo Flag1
  19. End If
  20.  
  21.  
  22. Flag1:
  23.     SSNReplace = “”    ‘reset (clear) variable for reuse
  24.     Exit Sub
  25.  
  26. Flag2:
  27.     MsgBox Err.Description
  28.     Resume Flag1:
  29.  
  30. End Sub
  31.  

OK, I input and tested the code. It's not quite right. Here is what happens:

When I first click the command button , I get the input box asking for a SSN. If I enter NO data and click the OK or Cancel button, I get this message:
FindRecord action requires a Find what argument. (I'm guessing that is coming from Flag 2) Also, is there a way for the systerm to just exit if someone were to click the CANCEL button???

Secondly, I closed the databse and then opened it again and started from scratch. I input a SSN and clicked OK. When I do, I get this message:
You can't use find or replace now. (Flag 2 again, I assume)
Jul 17 '07 #9
puppydogbuddy
1,923 Recognized Expert Top Contributor
OK, I input and tested the code. It's not quite right. Here is what happens:

When I first click the command button , I get the input box asking for a SSN. If I enter NO data and click the OK or Cancel button, I get this message:
FindRecord action requires a Find what argument. (I'm guessing that is coming from Flag 2) Also, is there a way for the systerm to just exit if someone were to click the CANCEL button???

Secondly, I closed the databse and then opened it again and started from scratch. I input a SSN and clicked OK. When I do, I get this message:
You can't use find or replace now. (Flag 2 again, I assume)
No, the main problem was that the code had no responses to the message box. Although I could not test it, this version should work much better. Try it and let me know.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FindSSN_Click()
  2. On Error GoTo Error_Routine
  3.  
  4. Dim SSNreplace As String
  5. Dim iReturn As Integer            'captures response from the message box
  6.  
  7.  
  8.  
  9. DoCmd.GoToControl "SSN"           'Assumes your control bound to the SSN field in the table is named SSN
  10. SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
  11. DoCmd.FindRecord SSNreplace, acAnywhere, , acSearchAll, , acCurrent, True
  12. If SSNreplace = "" Then
  13.     iReturn = MsgBox("No Number Entered. Please Try Again", vbCritical Or vbOKCancel, "Update a SSN")
  14. ElseIf SSNreplace = SSN Then
  15.     iReturn = MsgBox("Match Found For: " & SSNreplace, vbInformation Or vbOKCancel, "Update a SSN")
  16. Else
  17.     iReturn = MsgBox("Sorry, SSN NOT Found. Please Try Again", vbCritical Or vbOKCancel, "Update a SSN")
  18. End If
  19. If iReturn = 1 Then
  20.     Exit Sub
  21. Else
  22.    GoTo Exit_Continue
  23. End If
  24.  
  25. Exit_Continue:
  26.         SSNreplace = "" 'reset (clear) variable for reuse
  27.         Exit Sub
  28. Error_Routine:
  29.         MsgBox "Error# " & Err.Number & " " & Err.Description
  30.         Resume Exit_Continue
  31.  
  32. End Sub
  33.  
Jul 18 '07 #10
Proaccesspro
132 New Member
No, the main problem was that the code had no responses to the message box. Although I could not test it, this version should work much better. Try it and let me know.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FindSSN_Click()
  2. On Error GoTo Error_Routine
  3.  
  4. Dim SSNreplace As String
  5. Dim iReturn As Integer            'captures response from the message box
  6.  
  7.  
  8.  
  9. DoCmd.GoToControl "SSN"           'Assumes your control bound to the SSN field in the table is named SSN
  10. SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
  11. DoCmd.FindRecord SSNreplace, acAnywhere, , acSearchAll, , acCurrent, True
  12. If SSNreplace = "" Then
  13.     iReturn = MsgBox("No Number Entered. Please Try Again", vbCritical Or vbOKCancel, "Update a SSN")
  14. ElseIf SSNreplace = SSN Then
  15.     iReturn = MsgBox("Match Found For: " & SSNreplace, vbInformation Or vbOKCancel, "Update a SSN")
  16. Else
  17.     iReturn = MsgBox("Sorry, SSN NOT Found. Please Try Again", vbCritical Or vbOKCancel, "Update a SSN")
  18. End If
  19. If iReturn = 1 Then
  20.     Exit Sub
  21. Else
  22.    GoTo Exit_Continue
  23. End If
  24.  
  25. Exit_Continue:
  26.         SSNreplace = "" 'reset (clear) variable for reuse
  27.         Exit Sub
  28. Error_Routine:
  29.         MsgBox "Error# " & Err.Number & " " & Err.Description
  30.         Resume Exit_Continue
  31.  
  32. End Sub
  33.  

Same results except the error messages now have a number: 2142 & 2137.

How does iReturn get set to 1???
Jul 18 '07 #11
Proaccesspro
132 New Member
Same results except the error messages now have a number: 2142 & 2137.

How does iReturn get set to 1???
When I set thedebugger, the code jumps from this line:

DoCmd.GoToControl "SSN" 'Assumes

to the Error routine. Although I still get the input box.
Jul 18 '07 #12
puppydogbuddy
1,923 Recognized Expert Top Contributor
When I set thedebugger, the code jumps from this line:

DoCmd.GoToControl "SSN" 'Assumes

to the Error routine. Although I still get the input box.
1. In your posts in the beginning you indicated that the name of the field for the social security number in your table was PersonID, the later on SSN was used, I am assuming that SSN is the name of the field where the valid social security nos are stored. Is this correct?
2.. the comment: 'Assumes your control bound to the SSN field in the table is named SSN is supposed to be a non-executable comment, but apparently is being commingled with the executable part of the line.

Change this:
DoCmd.GoToControl "SSN" 'Assumes your control bound to the SSN field in the table is named SSN

To this (place comment on a separate line from the executable code):

'Assumes your control bound to the SSN field in the table is named SSN
DoCmd.GoToControl "SSN"

3. the value of 1 for the reponse to the button response is the constant value for the vbOkay button in Access. Each button has a constant value associated to it. This should be explained in more detail in Access on-line help.

Let me know what happens after you make the change discussed in #2 above and recompile.
Jul 18 '07 #13
Proaccesspro
132 New Member
1. In your posts in the beginning you indicated that the name of the field for the social security number in your table was PersonID, the later on SSN was used, I am assuming that SSN is the name of the field where the valid social security nos are stored. Is this correct?
2.. the comment: 'Assumes your control bound to the SSN field in the table is named SSN is supposed to be a non-executable comment, but apparently is being commingled with the executable part of the line.

Change this:
DoCmd.GoToControl "SSN" 'Assumes your control bound to the SSN field in the table is named SSN

To this (place comment on a separate line from the executable code):

'Assumes your control bound to the SSN field in the table is named SSN
DoCmd.GoToControl "SSN"

3. the value of 1 for the reponse to the button response is the constant value for the vbOkay button in Access. Each button has a constant value associated to it. This should be explained in more detail in Access on-line help.

Let me know what happens after you make the change discussed in #2 above and recompile.
1. Yes, the field name is SSN (PersonID is label on the form)

2. I deleted the comment

3. It did not work at first....then suddenly it partially worked..(it would tell you if it found a match or not, if you failed to input any data, it would give the error "find action requires argument......" Now NOTHING works..just get the same 2 errors as before!!
Jul 18 '07 #14
puppydogbuddy
1,923 Recognized Expert Top Contributor
1. Yes, the field name is SSN (PersonID is label on the form)

2. I deleted the comment

3. It did not work at first....then suddenly it partially worked..(it would tell you if it found a match or not, if you failed to input any data, it would give the error "find action requires argument......" Now NOTHING works..just get the same 2 errors as before!!
Could you zip and email your mdb to me? If you download my vCard from my scripts profile, you will see my email address. Remove any sensitive info before sending. My version of Access is Access 2000. I will be able to fix it right away once I have it in front of me.
Jul 18 '07 #15
shortyt303
1 New Member
I am trying to replace text in a field with a blank space and need code to put in a macro to get that done. Any suggestions??
Jul 19 '07 #16
Proaccesspro
132 New Member
Could you zip and email your mdb to me? If you download my vCard from my scripts profile, you will see my email address. Remove any sensitive info before sending. My version of Access is Access 2000. I will be able to fix it right away once I have it in front of me.

I will email the DB today....Thanks!
Jul 24 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8700
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
10
3105
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
5
2617
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
1
1660
by: Tomomichi Amano | last post by:
Hello. I want to make replace & search functions in my text editor. Thanks to the kind people here at the newsgroup, I was able to make the function. But I was not able to understand how to...
3
8232
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
2
2719
by: FP | last post by:
I have a javascript variable set to the contents of a database comments field. To set the js variable I used the PHP addslashes function which encodes the apostrophe, double quotes and the...
6
2660
by: DataSmash | last post by:
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! #...
2
5052
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
1
2788
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
0
7098
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...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
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...
0
7470
tracyyun
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...
1
5026
isladogs
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...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.