Jag skulle behöva hjälp med hur man tar fram ett filnamn på en fil i access. Varför ska man maila dig lösningen? Då kan ingen anna ta del av den. JAg föreslår att du använder Common Dialogs API anrop. om jag vill ha in filnamnet i txtFilnamn.Value <code>Hur tar man reda på ett filnamn med ett knapptryck
Det jag hade tänkt mig var att man skulle trycka på en knapp som i sin tur
öppnade en Dialog Box där man sedan fick välja ett filnamn. När man sedan
val och tryckt på ok ska dialogrutan stängas och filnamnet ska skrivas in i en
textruta.
Är det någon som har någon lösning så kan ni vara så snäll och mejla mig.
Tack på förhand Thomas SwedinSv: Hur tar man reda på ett filnamn med ett knapptryck
Placera följande kod överst i formulärets classmodul eller i en separat modul:
<code>
Private Const MAX_PATH As Long = 255
Private Const OFN_READONLY As Long = &H1
Private Const OFN_OVERWRITEPROMPT As Long = &H2
Private Const OFN_HIDEREADONLY As Long = &H4
Private Const OFN_NOCHANGEDIR As Long = &H8
Private Const OFN_SHOWHELP As Long = &H10
Private Const OFN_ENABLEHOOK As Long = &H20
Private Const OFN_ENABLETEMPLATE As Long = &H40
Private Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
Private Const OFN_NOVALIDATE As Long = &H100
Private Const OFN_ALLOWMULTISELECT As Long = &H200
Private Const OFN_EXTENSIONDIFFERENT As Long = &H400
Private Const OFN_PATHMUSTEXIST As Long = &H800
Private Const OFN_FILEMUSTEXIST As Long = &H1000
Private Const OFN_CREATEPROMPT As Long = &H2000
Private Const OFN_SHAREAWARE As Long = &H4000
Private Const OFN_NOREADONLYRETURN As Long = &H8000
Private Const OFN_NOTESTFILECREATE As Long = &H10000
Private Const OFN_NONETWORKBUTTON As Long = &H20000
Private Const OFN_NOLONGNAMES As Long = &H40000
Private Const OFN_EXPLORER As Long = &H80000
Private Const OFN_NODEREFERENCELINKS As Long = &H100000
Private Const OFN_LONGNAMES As Long = &H200000
Private Const OFN_ENABLEINCLUDENOTIFY As Long = &H400000
Private Const OFN_ENABLESIZING As Long = &H800000
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitializeDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long
Public Function OpenDialog(Optional FileName As String, Optional Title As String, Optional Filter As String = "All files(*.*)|*.*", Optional InitialDir As String, Optional hwnd As Long) As String
Dim OFName As OPENFILENAME
OFName.lStructSize = LenB(OFName)
If hwnd Then
OFName.hwndOwner = hwnd
Else
OFName.hwndOwner = hWndAccessApp
End If
OFName.lpstrFilter = Replace(Filter, "|", vbNullChar) & vbNullChar & vbNullChar
OFName.nFilterIndex = 1
OFName.lpstrFile = String$(MAX_PATH, 0)
'LSet OFName.lpstrFile = FileName
OFName.nMaxFile = MAX_PATH
OFName.lpstrFileTitle = String$(MAX_PATH, 0)
OFName.nMaxFileTitle = MAX_PATH
If Len(InitialDir) Then
OFName.lpstrInitializeDir = InitialDir
Else
OFName.lpstrInitializeDir = CurDir$
End If
OFName.lpstrTitle = Title
OFName.flags = OFN_HIDEREADONLY Or OFN_SHAREAWARE Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
If GetOpenFileName(OFName) Then
OpenDialog = RTrim$(OFName.lpstrFile)
Else
Err.Raise 18, "OpenDialog", "Dialog was canceled"
End If
End Function
</code>
I knappen anropar du den sedan så här:
<code>
Private Sub Kommandoknapp2_Click()
On Error GoTo ErrHandler
Text0 = OpenDialog("" & Text0)
Exit Sub
ErrHandler:
Select Case Err.Number
Case 18
Resume Next
Case Else
MsgBox Err.Description, vbCritical, Err.Source
Resume Next
End Select
End Sub
</code>Sv: Hur tar man reda på ett filnamn med ett knapptryck
var skriver jag det och hur?Sv: Hur tar man reda på ett filnamn med ett knapptryck
Private Sub Kommandoknapp2_Click()
On Error GoTo ErrHandler
txtFilnamn.Value = OpenDialog("" & Text0)
Exit Sub
ErrHandler:
Select Case Err.Number
Case 18
Resume Next
Case Else
MsgBox Err.Description, vbCritical, Err.Source
Resume Next
End Select
End Sub
</code>