Results 1 to 18 of 18

Thread: [RESOLVED] How to get and save text from text box ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Resolved [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.

    How can i fix this?

    I work with VB 6.0

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to get and save text from text box ?

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    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
    So where the problem could be?

    As I said, the form is called from an other form.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to get and save text from text box ?

    What exactly do you mean when you say the form is called from another form?

    Also you can use the text values directly like:

    Code:
    Option Explicit
    
    1. Private sub cmdOK_Click()
    
    4. Open "C:\First.txt" For Output As #1 
         5. Print #1, txtTargetName.Text
    
    6. End sub

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to get and save text from text box ?

    Quote Originally Posted by Morris_Zg View Post
    But I have other variables that are different names (souch as PrTy, PrHe, Re) and it is the same with them too.
    ....
    No, "point" is a word reserved by VB6 and you shouldn't use it for your own variable. The others aren't.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    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?

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to get and save text from text box ?

    Quote Originally Posted by Morris_Zg View Post
    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.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to get and save text from text box ?

    Quote Originally Posted by Morris_Zg View Post
    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.

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: How to get and save text from text box ?

    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.
    Doctor Ed

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    Here is the attachment. I've done some changes.

    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".

    I hope I explained well this time.
    Attached Files Attached Files

  11. #11

  12. #12

  13. #13

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to get and save text from text box ?

    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.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    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.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    Quote Originally Posted by MartinLiss View Post
    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)

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to get and save text from text box ?

    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.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Location
    Zagreb
    Posts
    29

    Re: How to get and save text from text box ?

    Yes, you're right. User changes variables from frmSaveAsTarget for each measured point so I will try this with form name!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width