Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a module/function for Similar Repeated code

Status
Not open for further replies.

replyrpatil

Programmer
Apr 28, 2007
24
US
I have 35 fields and 35 images associated to these fields ( Example filed name is f1 then the Image/Picture filed associated to it is named as IMGAUGE )

Let us say if other filed names are f2 , f3 ………..f35

I don’t want to copy paste the same code 35 times ( i.e. for f2 I will select the code and replace f1 with f2)


IS THERE A WAY TO WRITE A SINGEL MODULE and call it as I need.

For example I want to create a single function in module say name “ImgCheck”

Then in call it like this….

Private Sub f1_AfterUpdate()
Call ImgCheck (f1)
End Sub

Private Sub f2_AfterUpdate()
Call ImgCheck (f2)
End Sub

And so on…………………

I tried to write a module but gives me error “ Compile Error : ByRef argument type mismatch)


Following is my code that I need to convert to module/function that I can call

*******************************************************************
Private Sub f1_AfterUpdate()
Dim strgtif As String
Dim tifverfy As String
Dim ImagePath As String
ImagePath = "C:\ BAS\"
If Nz(Me.f1, "") <> "" Then
strgtif = Me.f1
tifverfy = Right([strgtif], 4)

If tifverfy = ".TIF" Then
Forms!FrmProductInformation![IMf1].Picture = ImagePath & Me.f1
Else
Forms!FrmProductInformation![IMf1].Picture = "(none)"
End If
Else
Forms!FrmProductInformation![IMf1].Picture = "(none)"
End If
End Sub
*****************
 
Something like this ?
Code:
Sub ImgCheck(fx As Control)
Dim ImagePath As String
Dim IMfx As Control
Set IMfx = Forms!FrmProductInformation.Controls("IM" & fx.Name)
ImagePath = "C:\ BAS\"
If UCase(Right(fx.Value & "", 4)) = ".TIF" Then
    IMfx.Picture = ImagePath & fx.Value
Else
    IMfx.Picture = "(none)"
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

I am using this in event afterupdate , when i tried your code it gives me error “ Compile Error : ByRef argument type mismatch)

I have copied the code in module , made it as public function.

then in field f1 after update called it like

Private Sub f1_AfterUpdate()
Call ImgCheck (f1)
End Sub

but gives me the compiler error

 
Thank you PHV

your code worked perfectly ........... I had a typo in my filed name when calling the function that is wny i was getting “ Compile Error : ByRef argument type mismatch)




Thank you once again


Thanks
 
Dear PHV can you please help me convert this in module


I need to compare two image reports side by side and if there is a change I need to indicate if there is change or no change.

Main report has two sub report (rptPIactiveImages and rptPIInProcessImages )

rptPIactiveImages has following filed name (All the four have height set to zero.)

1) FiledName
2) Filed Image ( which is besides the filed name and the Image is named as “IMfiledName”)
3) Image Change NoChange ( Which is besides the Filed Image that puts a greenArrow image for no change and redarrow image for change) this filed is named as “IMCfiledName”
4) Push Field --- Because I cannot set the Image to Can Grow and Can Shrink , I have put a filed beside the IMCfiledName , if there is a image I am changing the its value that will push the next image down. This filed has Can Grow value set to true. This is named as “exFiledName”

rptPIInProcessImages is copy paste of the rptPIactiveImages and later I have deleted “Image Change NoChange” filed.


rptPIactiveImages and rptPIInProcessImages is pulling information from two different query based on same table with different filter criteria ( One filters the ACTIVE information and other filters INPROCESS information)

What I am doing is just comparing these Image values and showing them side by side .

The following code works fine for me but the problem is that I have about 37 different filed values that I need to compare and I do not want to Copy Paste the same code 37 time just changing the filed values.

I WANT TO CONVER THIS CODE IN A MODULE AND CALL IT LIKE

Call VerifyIMGChange (CASETIF)

Call VerifyIMGChange (DATATIF)


Etc……………………………..



If Nz(Me.rptPIactiveImages.Report.CASETIF, "") <> "" Or Nz(Me.rptPIInProcessImages.Report.CASETIF, "") <> "" Then
If Nz(Me.rptPIactiveImages.Report.CASETIF, "") <> "" Then
s1 = Me.rptPIactiveImages.Report.CASETIF
Else
s1 = "notanimage"
End If
If Nz(Me.rptPIInProcessImages.Report.CASETIF, "") <> "" Then
s2 = Me.rptPIInProcessImages.Report.CASETIF
Else
s2 = "notanimage"
End If
img1 = Right([s1], 4)
img2 = Right([s2], 4)
If img1 = ".TIF" Or img2 = ".TIF" Then
Me.rptPIactiveImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.ImcCASETIF.Picture = "(none)"
Me.rptPIInProcessImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.CASETIF.Visible = True
Me.rptPIactiveImages.Report.ImCASETIF.Visible = True
Me.rptPIactiveImages.Report.ImcCASETIF.Visible = True
Me.rptPIactiveImages.Report.exCASETIF.Visible = True
Me.rptPIInProcessImages.Report.CASETIF.Visible = True
Me.rptPIInProcessImages.Report.ImCASETIF.Visible = True
Me.rptPIInProcessImages.Report.exCASETIF.Visible = True
Me.rptPIactiveImages.Report.ImCASETIF.Height = 2800
Me.rptPIactiveImages.Report.ImCASETIF.Width = 3550
Me.rptPIactiveImages.Report.ImcCASETIF.Height = 2800
Me.rptPIactiveImages.Report.ImcCASETIF.Width = 1200
Me.rptPIInProcessImages.Report.ImCASETIF.Height = 2800
Me.rptPIInProcessImages.Report.ImCASETIF.Width = 3550
Me.rptPIactiveImages.Report.exCASETIF = "VVVVVVVVVVVVV"
Me.rptPIInProcessImages.Report.exCASETIF = "VVVVVVVVVVVVV"
If img1 = ".TIF" Then
Me.rptPIactiveImages.Report.[ImCASETIF].Picture = ImagePath & Me.rptPIactiveImages.Report.CASETIF
Else
Me.rptPIactiveImages.Report.ImCASETIF.Picture = ImagePath & "NoImageAvailable.tif"
End If
If img2 = ".TIF" Then
Me.rptPIInProcessImages.Report.[ImCASETIF].Picture = ImagePath & Me.rptPIInProcessImages.Report.CASETIF
Else
Me.rptPIInProcessImages.Report.ImCASETIF.Picture = ImagePath & "NoImageAvailable.tif"
End If
If Me.rptPIactiveImages.Report.CASETIF = Me.rptPIInProcessImages.Report.CASETIF Then
Me.rptPIactiveImages.Report.[ImcCASETIF].Picture = ImagePath & "GreenArrow.bmp"
Else
Me.rptPIactiveImages.Report.[ImcCASETIF].Picture = ImagePath & "redarrow.bmp"
End If
Else
Me.rptPIactiveImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.ImcCASETIF.Picture = "(none)"
Me.rptPIInProcessImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.CASETIF.Visible = False
Me.rptPIactiveImages.Report.ImCASETIF.Visible = False
Me.rptPIactiveImages.Report.ImcCASETIF.Visible = False
Me.rptPIactiveImages.Report.exCASETIF.Visible = False
Me.rptPIInProcessImages.Report.CASETIF.Visible = False
Me.rptPIInProcessImages.Report.ImCASETIF.Visible = False
Me.rptPIInProcessImages.Report.exCASETIF.Visible = False
End If
Else
Me.rptPIactiveImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.ImcCASETIF.Picture = "(none)"
Me.rptPIInProcessImages.Report.ImCASETIF.Picture = "(none)"
Me.rptPIactiveImages.Report.CASETIF.Visible = False
Me.rptPIactiveImages.Report.ImCASETIF.Visible = False
Me.rptPIactiveImages.Report.ImcCASETIF.Visible = False
Me.rptPIactiveImages.Report.exCASETIF.Visible = False
Me.rptPIInProcessImages.Report.CASETIF.Visible = False
Me.rptPIInProcessImages.Report.ImCASETIF.Visible = False
Me.rptPIInProcessImages.Report.exCASETIF.Visible = False
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top