|
-
Apr 22nd, 2001, 04:16 PM
#1
Thread Starter
Lively Member
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
?????
-
Apr 22nd, 2001, 04:22 PM
#2
Which line does VB highlight / pick out
Did you dim the Text1 variable ?
-
Apr 22nd, 2001, 04:24 PM
#3
Thread Starter
Lively Member
It highlights
Get #1, , Text1
-
Apr 22nd, 2001, 04:27 PM
#4
Frenzied Member
What is Text1.....a variable or a textfield????
-
Apr 22nd, 2001, 04:28 PM
#5
Thread Starter
Lively Member
A text field, which I want the text from cd.filename to be loaded into.
-
Apr 22nd, 2001, 04:30 PM
#6
Hyperactive Member
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
-
Apr 22nd, 2001, 04:36 PM
#7
Thread Starter
Lively Member
I dont think I can do it.. I always get Bad file Mode. =\
-
Apr 22nd, 2001, 04:36 PM
#8
Hyperactive Member
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
Last edited by John Yingling; Apr 22nd, 2001 at 06:04 PM.
-
Apr 22nd, 2001, 05:53 PM
#9
Thread Starter
Lively Member
that doesnt seem to work either, you make that a public sub in a bas file, am I right ?
-
Apr 22nd, 2001, 06:01 PM
#10
Hyperactive Member
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.
-
Apr 22nd, 2001, 06:03 PM
#11
Hyperactive Member
My fault, But you should have been able to fix it from the syntax error.
Use FUNCTION, not SUB.
-
Apr 22nd, 2001, 07:18 PM
#12
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
-
Apr 22nd, 2001, 07:30 PM
#13
Thread Starter
Lively Member
YES! thats works ... thanks !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|