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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a public variable to code that sets a form icon 1

Status
Not open for further replies.

votegop

Technical User
Feb 22, 2002
127
0
0
US
The below code works great, but it relies on a string (in red)holding the absolute path to the icon file.
I want to pass a public variable (I've already defined it, and it works elsewhere) that returns the relative application path. This is where the icon file will be.
If I have my global public variable "varFilePath", how do I pass it so that it will work the same as hard-coding the path inside quotes?

The Global Module
Option Compare Database

Private Const WM_SETICON = &H80
Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10
Private Const SM_CXSMICON As Long = 49
Private Const SM_CYSMICON As Long = 50

Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" _
(ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, _
ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Function SetFormIcon(hwnd As Long, strIconPath As String) As Boolean
Dim lIcon As Long
Dim lResult As Long
Dim x As Long, y As Long

x = GetSystemMetrics(SM_CXSMICON)
y = GetSystemMetrics(SM_CYSMICON)
lIcon = LoadImage(0, strIconPath, 1, x, y, LR_LOADFROMFILE)
lResult = SendMessage(hwnd, WM_SETICON, 0, ByVal lIcon)
End Function

The call
Private Sub Form_Load()
SetFormIcon Me.hwnd, "C:\MyFolder\MyIcon.ico"
End Sub

This does NOT work because I get "ByRef Argument Type Mismatch":
Private Sub Form_Load()
SetFormIcon Me.hwnd, varIconPathandFileName
End Sub

----
By the way, varIconPathandFileName is declared as String
----
Thanks for any help.
Jay
 
In older versions of Access (97 and older), you use
Code:
Left(CurrentDB().Name, Len(CurrentDB().Name) - Dir(CurrentDB().Name))


For newer versions of Access, I think there's something like Project.CurrentPath or something like that gives you the same result. I don't have it in front of me so I can't verify that, but it's something extremely similar.


Pete
 
Small oops. I noticed my reference to "varFilePath" at the top of my post should read "varIconPathandFileName". No difference.

Pete:
Thanks. I don't have a problem getting the relative file path into a public variable...I use:
-----
Public Static Function GetPathtoApplicationFolder() x = CurrentProject.FullName
'Then you can search for the last "\" in the string:
For I = Len(x) To 1 Step -1
If Mid(x, I, 1) = "\" Then
JustPath = Left(x, I)
varIconPathAndFileName = JustPath & "MyIcon.ico"
Exit For
End If
Next
End Function
------
GetPathToApplicationFolder gets called in the OnOpen event of the splash form.
My question I think is actually pretty simple (unfortunately I'm a simple person): Why won't this work?:
SetFormIcon Me.hwnd, varIconPathandFileName
It has something to do with data type.
 
My bad. I misread the intent, and you were pretty clear about the whole thing.

About the ByRef/ByVal mismatch--I don't know the root cause, as a string literal expression should be the same as a string variable expression.

Do a few things for me though:

1. Add the "Option Explicit" line at the top of this module.

2. Recompile the module.

If it is what I think it is, the parser can't figure out what your global variable varIconPathandFileName is supposed to be, so it defaults to Variant, which is set to Nothing (or null?), and it doesn't know how to interpret this as a string. Make sure your global is definitely concretely declared in the other module so that you can use it in this one.

If I'm wrong, then I'm wrong again, so no sweat on my part.


Pete
 
Pete:

Not wrong this time. Spot on! After I added Option Explicit, I had to declare x as string and I as integer.
Errors went away & all is joy.

Thank you kindly for the help!

Jay [roll1]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top