Question about using "Instr" in Access VBA

adibakale

Board Regular
Joined
Apr 10, 2015
Messages
52
I am pretty new to VBA and managed to write some code to export some customer information to word similar to a mailmerge function. In my Access database, The customer name shows as:

LastName, FirstName Middle Initial
Smith, John A

I use the Instr function to use the comma position and export the data to word so that it shows in this format:

FirstName, Middle Initial LastName
John A Smith

There are some instances where there is no comma in the customer name. When this happens, the code stops running and doesn't continue to export information into the word document.

I have tried using If - Then - Resume Next but I have been unable to figure this out. Any help would be greatly appreciated.

Here is my code:

Dim commaposition As Integer

commaposition = InStr(Forms!frm_main!CustomerName, ",")

.ActiveDocument.Tables(2).Cell(1, 1).Select
.Selection.Text = Mid(CStr(Forms!frm_main!CustomerName), commaposition + 1)
.ActiveDocument.Tables(2).Cell(1, 2).Select
.Selection.Text = Left(CStr(Forms!frm_main!CustomerName), commaposition - 1)
.ActiveDocument.Tables(3).Cell(1, 1).Select



If there is no comma, the code stops running and does not send any more information to word.

What I need is to add code to address occurences were there is no comma.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Rich (BB code):
Rich (BB code):

commaposition = InStr(Forms!frm_main!CustomerName, ",")

if commaposition  = 0 then
    'just use the whole name?
else
  .ActiveDocument.Tables(2).Cell(1, 1).Select
  .Selection.Text = Mid(CStr(Forms!frm_main!CustomerName), commaposition + 1)
  .ActiveDocument.Tables(2).Cell(1, 2).Select
  .Selection.Text = Left(CStr(Forms!frm_main!CustomerName), commaposition - 1)
  .ActiveDocument.Tables(3).Cell(1, 1).Select
end if


 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,635
Members
449,043
Latest member
farhansadik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top