1

I know there's several posts which are similar to the one I am posting, and I've looked through them and still can't figure out what I am doing wrong. I've also checked out how to properly set up a CASE statement using VBA, but still can't find what I am doing wrong.

Basically, I have a user-entry form where the user enters a part number which looks something like this: 0 A1B2-3C-4D-5E6F. Once the user has added the record, the part number should scan the string for any " " or "-" and replace them with "", to look like this: A1B23C4D5E6F, and update the record in the table.

Here is the code I am using: (txtString as my field)

Public Function strReplace(txtString As Variant) As Variant

 Select Case txtString

        Case "-"
            strReplace = ""

        Case " "
            strReplace = ""

        Case Else
            strReplace = txtString

    End Select

End Function

I am still learning the ropes with VBA, but this compiles fine. Did I structure this code wrong, logically, for what I am trying to accomplish, or have I set up my code wrong? I appreciate all feedback and answers.

Thanks

EDIT This code is being called when frmAddString opens(within access' Macros & Code - Visual Basic - Form_frmAddString)

EDIT2 I am trying this in a testing database which has the following:

tblTestingStrings

-Field1: StringID

-Field2: txtString

frmAddString(My code is located on this Pop up form which opens in Add)

-btnSaveandClose

-txtString

-StringID

EDIT3 Code for frmAddString

Option Compare Database

Private Sub btnSaveandClose_Click()

DoCmd.Save
DoCmd.Close

End Sub

Public Function strReplace(txtString As String) As String

 Select Case txtString

        Case "-"
          strReplace = ""

        Case " "
            strReplace = ""

        Case Else
            txtString = strReplace

    End Select

End Function

txtString being the place where the user types in a word.

4
  • 1
    You are comparing the full string, you need to compare the characters
    – Barranka
    Sep 17, 2014 at 20:38
  • @Barranka Would that not be what I want to do though? As in when a user enters a full string, look it over and replace what is needed?
    – Dylan
    Sep 17, 2014 at 20:41
  • 1
    @Dylan, the entry from the user does not equal - or ` `; your code is built as if it were one character. You would need to split the entry into single characters and loop through that set, for example...not particularly efficient
    – Smandoli
    Sep 17, 2014 at 20:52
  • Ok I understand what I am doing wrong. I am going to review this code and see how to fix it.
    – Dylan
    Sep 17, 2014 at 21:11

1 Answer 1

4

It's actually easer that what you're doing.

Public Function strReplace(txtString As Variant) As Variant
        strReplace = Replace(Replace(txtString, " ", ""), "-", "")
End Function
12
  • 1
    This looks good, but I would recommend using two lines instead of one for simple legibility.
    – Smandoli
    Sep 17, 2014 at 20:53
  • I have a feeling that this also works, and when I tried it out by entering in 11-22-33, it still saved it in the table as 11-22-33. I am thinking it has something to do with the way my form is set up. I created a test database with the following: tblTestingStrings frmTestingStrings, frmAddString, where frmAddString simply adds prompts to add a new string. It also has a button with the DoCmd.Save and the DoCmd.Close.
    – Dylan
    Sep 17, 2014 at 20:54
  • @Dylan -- to prove this answer is indeed correct, paste this in your Immediate window and press Enter: ?Replace(Replace("My Sam ple en-try", " ", ""), "-", "")
    – Smandoli
    Sep 17, 2014 at 20:55
  • @Dylan -- try changing As Variant to As String
    – Smandoli
    Sep 17, 2014 at 20:56
  • 1
    @tbur, I took a different route as I was able to get it to do exactly what I want in a query, so I just made the form off the query, and made the query update the records on the table where needed. Worked like a charm! I think the way I set up the form caused the issues. Thanks again for the answer!
    – Dylan
    Sep 18, 2014 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.