2012-10-26 37 views
0

我有瀏覽按鈕變量和編碼內瀏覽按鈕事件。現在我必須在另一個按鈕事件中訪問這些變量。如何在vba中聲明?如何聲明全局變量來訪問vba中的其他函數?

private sub commandbutton1_click() 
Dim someFileName As Variant 
Dim folderName As String 
Dim i As Integer 
Const STRING_NOT_FOUND As Integer = 0 

'select a file using a dialog and get the full name with path included 
someFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt") 

If someFileName <> False Then 

'strip off the folder path 
folderName = vbNullString 
i = 1 

While STRING_NOT_FOUND < i 
    i = InStr(1, someFileName, "\", vbTextCompare) 'returns position of the first  backslash "\" 
    If i <> STRING_NOT_FOUND Then 
     folderName = folderName & Left(someFileName, i) 
     someFileName = Right(someFileName, Len(someFileName) - i) 
    Else 'no backslash was found... we are done 
     GetAFileName = someFileName 

    End If 
Wend 

Else 
GetAFileName = vbNullString 
End If 
end sub 

private sub commandbutton2_click() 

我必須在這裏訪問GetAFileName變量嗎?

+2

出於好奇 - 你爲什麼不接受的答案,就像在所有?許多用戶已經幫助過你,但是,你不會對任何事進行投票,接受任何問題或提供你自己的答案 - 你認爲這是公平的嗎? – Jook

回答

1

使用enter code here全球VARNAME

例: 「運行這個函數 「AAA」,並顯示有標題爲 「1」

Global A 
Function aaa() 
    A = 1 
    Call BBB 
End Function 

Function BBB() 
    MsgBox (A) 
End Function 

希望幫助一個消息框!

+1

+1,同樣的想法,你是第一個;) – Jook

+0

只是碰巧在正確的時間瀏覽問題,我猜; P – jpsfer

+0

真的不重要,但不建議使用全球。您應該使用Public(而不是同樣的事情),因爲Global僅僅是爲了向後兼容。雖然因爲兩個潛艇都在同一個模塊中,所以我會像Jook的答案一樣與Private一起去。 –

2

這將是你將不得不使用結構:

Option Explicit 
private GetAFileName as string 

private sub commandbutton1_click() 
    Dim some As Variant 
    some = "test2" 
    GetAFileName = some 
end sub 

private sub commandbutton2_click() 
    MsgBox GetAFileName 
end sub 

你必須定義這個GetAFileName之外你的函數,從兩者的訪問。

順便說一句 - 你應該使用option explicit來確保每個變量都有一個明確的定義。

+0

它很好,但我仍然沒有使用... – user441978

+1

我的編輯樣張基本功能 - 如果您仍然收到nullstring,那是因爲button1沒有被點擊,或者您沒有設置GetAFileName - 使用調試器。 – Jook

0
Option Compare Database 
Option Explicit 

Public Type BROWSEINFO 
hwndOwner As Long 
pidlRoot As Long 
pszDisplayName As String 
pszTitle As String 
ulFlags As Long 
lpfn As Long 
lParam As Long 
iImage As Long 
End Type 

Const MAX_PATH As Long = 260 
Const dhcErrorExtendedError = 1208& 
Const dhcNoError = 0& 

「的常量 指定瀏覽文件夾的根目錄」,您還可以通過爲searhcable文件夾和選項的常量指定的值。

Const dhcCSIdlDesktop = &H0 
Const dhcCSIdlPrograms = &H2 
Const dhcCSIdlControlPanel = &H3 
Const dhcCSIdlInstalledPrinters = &H4 
Const dhcCSIdlPersonal = &H5 
Const dhcCSIdlFavorites = &H6 
Const dhcCSIdlStartupPmGroup = &H7 
Const dhcCSIdlRecentDocDir = &H8 
Const dhcCSIdlSendToItemsDir = &H9 
Const dhcCSIdlRecycleBin = &HA 
Const dhcCSIdlStartMenu = &HB 
Const dhcCSIdlDesktopDirectory = &H10 
Const dhcCSIdlMyComputer = &H11 
Const dhcCSIdlNetworkNeighborhood = &H12 
Const dhcCSIdlNetHoodFileSystemDir = &H13 
Const dhcCSIdlFonts = &H14 
Const dhcCSIdlTemplates = &H15 

「常數限制選項爲BrowseForFolder對話框

Const dhcBifReturnAll = &H0 
Const dhcBifReturnOnlyFileSystemDirs = &H1 
Const dhcBifDontGoBelowDomain = &H2 
Const dhcBifIncludeStatusText = &H4 
Const dhcBifSystemAncestors = &H8 
Const dhcBifBrowseForComputer = &H1000 
Const dhcBifBrowseForPrinter = &H2000 

」 ......你可以從你的集成API瀏覽器這些值的更多恆定specifcation或去AllPai.net和看他們的樣品。

Public Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BROWSEINFO) As Long 

'修正

Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _ 
(ByVal hwndOwner As Long, ByVal nFolder As Long, ByRef pidl As Long) As Long 



Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long 

Public Function BrowseForFolder(ByVal lngCSIDL As Long, _ 
ByVal lngBiFlags As Long, _ 
strFolder As String, _ 
Optional ByVal hWnd As Long = 0, _ 
Optional pszTitle As String = "Select Folder") As Long 


Dim usrBrws As BROWSEINFO 
Dim lngReturn As Long 
Dim lngIDL As Long 

If SHGetSpecialFolderLocation(hWnd, lngCSIDL, lngIDL) = 0 Then 

' 在這裏設置瀏覽結構

With usrBrws 
    .hwndOwner = hWnd 
    .pidlRoot = lngIDL 
    .pszDisplayName = String$(MAX_PATH, vbNullChar) 
    .pszTitle = pszTitle 
    .ulFlags = lngBiFlags 
End With 

'打開對話框

lngIDL = SHBrowseForFolder(usrBrws) 

If lngIDL = 0 Then Exit Function 

' 如果成功的話

If lngIDL Then strFolder = String$(MAX_PATH, vbNullChar) 

    'resolve the long value form the lngIDL to a real path 

    If SHGetPathFromIDList(lngIDL, strFolder) Then 
     strFolder = Left(strFolder, InStr(1, strFolder, vbNullChar)) 
    lngReturn = dhcNoError 'to show there is no error. 
    Else 
     'nothing real is available. 
     'return a virtual selection 
     strFolder = Left(usrBrws.pszDisplayName, InStr(1, usrBrws.pszDisplayName, vbNullChar)) 
    lngReturn = dhcNoError 'to show there is no error. 
    End If 
Else 
lngReturn = dhcErrorExtendedError 'something went wrong 
End If 


BrowseForFolder = lngReturn 

End Function 
0
Sub hth() 

With Application.FileDialog(msoFileDialogFolderPicker) 
    .AllowMultiSelect = False 
    .Show 

    If .SelectedItems.Count > 0 Then 
txt2.setfocus 
     txt2.Text = .SelectedItems(1) 
    End If 

End With 

End Sub 
0
Private Sub Command9_Click() 
Call BrowseForFolder(dhcCSIdlDesktop, dhcBifReturnOnlyFileSystemDirs, _ 
STRPATH2, pszTitle:="Select a folder:") 
If STRPATH2 <> "" Then 
STRPATH2 = Left(STRPATH2, Len(STRPATH2) - 1) 
Text7.Value = STRPATH2 
'DoCmd.Close acForm, "frm_generate_report", acSaveNo 
'DoCmd.OpenForm "frm_generate_report", acNormal 
End If 
End Sub