[RESOLVED] How to get and save text from text box ?
Hi,
I have a frame with text boxes and ok and cancel click which is called from an other frame. I want to save the text that user enters in textbox called "txtTargetName" in a file. I tried to define the content of one text box (for test) as:
Code:
Option Explicit
1. Private sub cmdOK_Click()
2. dim Point as string
3. 'here I open file as #1 for output
4. Point=txtTargetName.Text
5. print #1, Point
6. End sub
but it says "Type mismatch". If I put "dim Point..." after option explicit and before private sub program says type missmatch, also. And if I put it in mother frame, it says that the variable is not defined.
Reason for "type mismatch" error is bad naming convention - Point is a method (applies to form or picturebox objects) that gets rgb color for specified x/y coordinates.
There is also no code that actually opens/closes the file... Try this instead:
Code:
Dim strText As String
Dim iFileNum As Integer
strText = txtTargetName.Text
iFileNum = FreeFile
Open App.Path & "\mydata.txt" For Output As iFileNum
Print iFileNum, strText
Close iFileNum
But I have other variables that are different names (souch as PrTy, PrHe, Re) and it is the same with them too.
The code is
Code:
0. Option Explicit
1. Private sub cmdOK_Click()
2. Dim PointName as String
3. PointName=txtTargetName.Text
4. Open "C:\First.txt" For Output As #1
5. Print #1, PointName
6. End sub
I meant to say that when user presses a command button on another form, the form we are talking about shows. there is to fulfill several text boxes and when ok button is pressed the entered text must be saved. I can easily change the name of the PointName variable into something else, but it doesn't work for other variables, as well. so i'm not sure if naming is the only problem
could it be that the problem is with local or global definition of the variables?
I meant to say that when user presses a command button on another form, the form we are talking about shows...
I thought we are talking about the "type mismatch" error you were getting and I explained (with example) what is the cause and how to "fix" it.
What other form you are talking about I have no idea - perhaps you can elaborate a bit more.
Last edited by RhinoBull; Oct 21st, 2010 at 06:25 PM.
I meant to say that when user presses a command button on another form, the form we are talking about shows. there is to fulfill several text boxes and when ok button is pressed the entered text must be saved. I can easily change the name of the PointName variable into something else, but it doesn't work for other variables, as well. so i'm not sure if naming is the only problem
could it be that the problem is with local or global definition of the variables?
Please create a zip or rar file from your complete project and attach it to a post. Once you've done that please describe the exact steps necessary to recreate the problem.
Let's get back to basics and try this. Build a form with a command button, a label, and a text box. Then apply this code:
Code:
Private Sub Command1_Click()
Text1.Text = "Hello, Morris Zg"
Open "MyTextFile.Txt" For Output As #1
Print #1, Text1.Text
Close #1
Open "MyTextFile.Txt" For Input As #1
Label1.Caption = Input$(LOF(1), #1)
Close #1
End Sub
Click on the command button as many times as you want.
So, I want that when user presses "Save Target" that the data are saved to file.
In frame "Measurement", there is "Sub cmdSaveTarget_Click()" in which stays the code for saving the variables.
There are 2 kind of variables:
1. from frame "Measurement" (but they aren't problem) and
2. from frame "SaveAsTarget" - and these are the problem. It seems that values of these variables aren't preserved to be saved in a text file.
And also, the file isn't beeing created even if I put the open/print/close code in frame "SaveAsTarget".
There seems to be a mixup in terminology. Did you maybe mean Form instead of Frame in post #10? There is a SetAsTarget form but I don't see anything that might be "SaveAsTarget" frame. Also, in your code you use frm prefix for Frame controls, which is used for Forms. Frames should have a "fra" prefix, ie fraInputFields.
On frmSetAsTarget in cmdOk_Click event the code to save the to a file is commented out. Is that what you are having trouble with?
One off topic comment about that code, don't hardcode file numbers (#1), instead use FreeFile function to get one. For more info check this link (namely section Using FreeFile) http://www.vb6.us/tutorials/how-read-simple-text-files
One more question, frmMeasure, cmdSaveTarget_Click event code, you just have a Print line. Where is that file Opened and Closed? You should usually do that in one block so there's no chance of trying to write to an unopened file or forgetting to close it properly.
Hey,
yes, I see now I've mixed terminology. I was saying frame instead of form.
About the file:
-the line open file is in "Sub cmdOnline_Click()" (Open "C:\Nulto mjerenje.txt" For Output As #1) and
-closing the file is in "Sub cmdOffline_Click()" (Close #1).
But, this subs don't execute until the instrument is connected to application (Go Online and Go Offline buttons). So, for test, I've put open/print/close in form frmSaveAsTarget. These lines are now commented out, because they are just for test. Even when I turned them on, they didnt work.
Conclusion, the issue is how to get data into form frmMeasurement from form frmSaveAsTarget. I think this is the problem. Could I be right?
I'll check the FreeFile function!
Last edited by Morris_Zg; Oct 23rd, 2010 at 03:49 AM.
I can't seem to find a frame named SaveAsTarget. What form is it on?
I also need complete, step by step, instructions on how to reproduce the problem.
Hi!
There is no frame named SaveAsTarget. I mixed terminology. There are FORMS frmSaveAsTarget and other.
All the steps for the user:
1. connecting the measuring instrument to computer
2. starting the application and choosing connecting parameters in form frmSetup
3. connecting instr with the application by pressing the "Go Online" button (here is the file being created)
4. pressing "Define Target" button and entering data about target in form frmSaveAsTarget
5. measuring with instr by pressing "Measure" button
6. saving the measures from instr that are in form frmMeasurement AND data from form frmSaveAsTarget by pressing "SaveTarget" button
7. ending by "Going Offline" (here is the file with data closed)
It looks like you are trying to save the data from variables with the same names in two different forms. To do that from code in frmMeasure you need to refer to the the variables in frmSetAsTarget using the form's name, for example frmSetAsTarget.TrgNa . In order to do that the variables in frmSetAsTarget need to be defined as Public. (Note that any time you want to refer to a variable on another form it has to be Public and you need to prepend the form name.)
A question I have for you is will the values for TrgNa be different? If so then what you are doing (with the addition of what I suggested above) will be okay, but if the values are the same then what you probably should do is to just have one place where TrgNa is defined and that is in a code module with the variable defined as Public. Either form can then refer directly to TrgNa without using any form name.
Last edited by MartinLiss; Oct 23rd, 2010 at 10:25 AM.