-
Could someone tell me why I get
'Variable Required - cant assign to this expression'
when I try to use this code from a command button.
Private Sub Command1_Click()
With CommonDialog
.ShowOpen
.Filter = "Text File|*.txt|"
End With
Open CommonDialog.filename For Input As #1
Do Until EOF(1) = True
Get #1, , Text1
Loop
Close #1
End Sub
?????
-
Which line does VB highlight / pick out
Did you dim the Text1 variable ?
-
It highlights
Get #1, , Text1
-
What is Text1.....a variable or a textfield????
-
A text field, which I want the text from cd.filename to be loaded into.
-
If text1 is a textbox then you supplied an object (forget about default propert TEXT here).
Dim strText as string
Get #1, , strText
text1.text = strText
-
I dont think I can do it.. I always get Bad file Mode. =\
-
Start getting used to using FileSystemObject. Open , Get and Put etc are going away in VB.Net in favor of objects.
text1.text = ReturnFileasString(CommonDialog.filename)
Code:
'* Public in Form, Standard Module or method of Class
Public Function ReturnFileAsString(byval strPathFile as string) as string
Dim objFS as object
Dim objTs as object
Dim strText as string
Set objFS = CreateObject("scripting.FileSystemObject")
if not objFs.FileExsts(strPathFile) then exit sub
'* Need ON ERROR to catch ACCESS DENIED etc
Set objTS = objFS.OpenTextFile(strPathFile, ForReading, False, False)
objTS.ReadAll strText
objts.close
Set objTs = nothing
Set objFS = nothing
ReturnFileAsString = strText
End Sub
-
that doesnt seem to work either, you make that a public sub in a bas file, am I right ?
-
PUBLIC Sub wherever, Form, Standard Module, Class.
What is the error message. "Doesn't work" is sort of nebulous.
"can't create component"
"file not found"
"object does not support..."
etc.
-
My fault, But you should have been able to fix it from the syntax error.
Use FUNCTION, not SUB.
-
Try this:
Code:
Private Sub Command1_Click()
Dim iFile As Integer
On Error GoTo User_Cancelled
With CommonDialog1
.CancelError = True
.DialogTitle = "Select a Text File.."
.Filter = "Text File (*.txt)|*.txt"
.ShowOpen
iFile = FreeFile
Open .filename For Input As iFile
Text1 = Input(LOF(iFile), iFile)
Close iFile
End With
User_Cancelled:
End Sub
-
YES! thats works ... thanks !