Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Passing a optional argument

Posted on 2007-11-30
Medium Priority
155 Views
Hi experts
I have 5 mshflexgrids on a form 3 are contained in pictureboxs I am trying adjust the grids widths, based on the column widths and am trying to also set the picture widths as optional argument. The code below works fine if i pass the picture argument, but fails if with don't with a "Object variable not set" error message. How can i correct this?

Sub AdjustGridWidth(grd As MSHFlexGrid, Optional pic As PictureBox)
Dim i As Integer
Dim iWidth As Integer

With grd
For i = 1 To .Cols - 1
iWidth = iWidth + .ColWidth(i) + 13
.Width = iWidth
Next i
End With

If IsMissing(pic) Then
Exit Sub
End If

With pic
pic.Width = iWidth
End With

End Sub
0
Question by:isnoend2001
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 93

Accepted Solution

Patrick Matthews earned 500 total points
ID: 20384022
Replace:

If IsMissing(pic) Then
Exit Sub
End If

with:

If pic Is Nothing Then
Exit Sub
End If

IsMissing only works whne the argument is defined as Variant.
0

LVL 38

Assisted Solution

PaulHews earned 500 total points
ID: 20384068
Try this instead.  IsMissing only works with variants.  (See the help.)
Sub AdjustGridWidth(grd As MSHFlexGrid, Optional pic As PictureBox = Nothing)
Dim i As Integer
Dim iWidth As Integer

With grd
For i = 1 To .Cols - 1
iWidth = iWidth + .ColWidth(i) + 13
.Width = iWidth
Next i
End With

If Not pic Is Nothing Then
With pic
pic.Width = iWidth
End With
End If
End Sub
0

Author Closing Comment

ID: 31411983
Thanks both work perfect
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll