I am making a new program (obviously). And I need some help. In this program, I have up to 23 text boxes. They are meant to edit a file. Not any type of file, just a file, with no extensions. There is text inside the file though.

I have tried opening the file in notepad, so I know it works. When I open it in notepad, it displays the text, then you can save it back to that same file, without changing the file type (as you all know, like editing an ini file). I want this program to have the same functions, but for each text box.

1) I have each text box (About 20) having an "Open button" and a "Save button." The program comes with a folder called readable files. I need a code that will open the file I want it to only. Say I want Textbox1 to open "Rabbit" file. When you hit the open command button, it opens without making you have to go to the files and search.

2) For the save button, All I want is a command button that saves to the file that was opened. NOT like a save as button. Then, I want a message box saying save complete.

Thank you in advance! Any help what so ever would be greatly appreciated!

Recommended Answers

All 8 Replies

I think you must be aware that the Rules and Regulations of posting. Helpers will help if you have done/tried something. Do you have written any codes about it? If you have please paste it so that helpers might correct your code.
Thanks

No. I'm really unsure were to start. I have tried common dialog, but I can't seem to get it to work. I have tried stuff like this

1.
      Private Sub Command1_Click()
   2.
          Open App.Path & "\data.txt" For Output As #1
   3.
              Print #1, Text1
   4.
              Print #1, Text2
   5.
              Print #1, Text3
   6.
          Close #1
   7.
      End Sub
   8.
   9.
      Private Sub Form_Load()
  10.
          Dim tmp As String
  11.
          If Len(Dir(App.Path & "\data.txt")) <> 0 Then
  12.
              Open App.Path & "\data.txt" For Input As #1
  13.
                  Line Input #1, tmp
  14.
                  Text1 = tmp
  15.
                  Line Input #1, tmp
  16.
                  Text2 = tmp
  17.
                  Line Input #1, tmp
  18.
                  Text3 = tmp
  19.
              Close #1
  20.
          End If
  21.
      End Sub

I cannot get it to work. I need each text box to have an open and a save. My codes shouldn't matter, I think they are the wrong codes to begin with. Basically, all I want is an open like in notepad, except it opens the file with the name it is looking for, automatically in the directory folder. And a save, that saves it directly to the file, without consulting you to do anything, unlike the save as, which lets you choose. Thanks so much. I'm really new here, and haven't gotten the ropes of things quite yet.

Try and use the following. Change the message boxes to what you need to do with the file. It opens a file, save a file, compare 2 files. Just play around until it suits your needs.

Option Explicit
'Description: Calls the "Open File Dialog" without need for an OCX

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
    lpstrInitialDir 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
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

'Place the following code in under a command button or in a menu, etc...
'This will open a file...
    
Private Sub Command1_Click()

Dim ofn As OPENFILENAME
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    ofn.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "Rich Text Files (*.rtf)" + Chr$(0) + "*.rtf" + Chr$(0)
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = CurDir
        ofn.lpstrTitle = "Our File Open Title"
        ofn.flags = 0
        Dim a
        a = GetOpenFileName(ofn)

        If (a) Then
                MsgBox "File to Open: " + Trim$(ofn.lpstrFile)
        Else
                MsgBox "Cancel was pressed"
        End If
End Sub

Private Sub Command2_Click()

'This will save a file

    Dim ofn As OPENFILENAME
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    ofn.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "Rich Text Files (*.rtf)" + Chr$(0) + "*.rtf" + Chr$(0)
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = CurDir
        ofn.lpstrTitle = "Our File Save Title"
        ofn.flags = 0
        Dim a
        a = GetOpenFileName(ofn)

        If (a) Then
                MsgBox "File to Save: " + Trim$(ofn.lpstrFile)
        Else
                MsgBox "Cancel was pressed"
        End If
End Sub

Private Sub Command3_Click()

'Description: Compares the content of two files
'If their sizes differ, save the newest using the above....

Open "file1" For Binary As #1
Open "file2" For Binary As #2
issame% = True
If LOF(1) <> LOF(2) Then
    issame% = False
Else
    whole& = LOF(1) \ 10000         'number of whole 10,000 byte chunks
        part& = LOF(1) Mod 10000        'remaining bytes at end of file
        buffer1$ = String$(10000, 0)
        buffer2$ = String$(10000, 0)
        start& = 1
        For x& = 1 To whole&            'this for-next loop will get 10,000
        Get #1, start&, buffer1$      'byte chunks at a time.
        Get #2, start&, buffer2$
        If buffer1$ <> buffer2$ Then
            issame% = False
                Exit For
        End If
        start& = start& + 10000
        Next
        buffer1$ = String$(part&, 0)
        buffer2$ = String$(part&, 0)
        Get #1, start&, buffer1$        'get the remaining bytes at the end
        Get #2, start&, buffer2$        'get the remaining bytes at the end
        If buffer1$ <> buffer2$ Then issame% = False
        End If
        Close
        If issame% Then
            MsgBox "Files are identical", 64, "Info"
        Else
                 MsgBox "Files are NOT identical", 16, "Info"
        End If
End Sub

That code is great, but only one problem. Each text box (20) will be opening a different file, and they each have a different Open and save button for there specific text. THANK YOU so much for the help. Maybe it just needs to be altered a little. I will keep messing with it. Thanks!

EDIT: I accidentally double posted, and couldn't figure out how to delete it.

It's a pleasure 3d. With the above code, use a select case statement to find which textbox has focus, a for next loop to determine the same etc. The code is a basis for you to start manipulating the files you want to open and edit.

If this helped, please mark as solved, thanks.

Sorry, once again. I need each text box to have there own code. One text box s meant to open one file, while another opens a completely different file. The code should maybe be like this:

Private Sub Command19_Click()
Save textbox19.text to: ........
End Sub

Private Sub Command20_Click()
Save textbox20.text to: ..........
End Sub

Not a good example, because it didn't work, but I would also like the open to be a variation like that. I know it isn't that simple... Or maybe it is. Thanks again, I'm sorry for the trouble.

Please help me. Its for my mom :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.