CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Save AS

  1. #1
    Join Date
    Aug 2014
    Posts
    4

    Save AS

    New to this sight and haven't been programming long but I've created a simple form with different kinds of entries on it in vb6. I've created a File menu with Save and Save As in it the only thing is I can't seem to find anywhere how to code the Save As function so I can save the form and go back later and edit entries in my form. If there is a Post or article on this site about this I would appreciate someone pointing me in that direction

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Save AS

    This should do it:

    Code:
    Option Explicit
    
    Public xlAppTemp As Excel.Application
    Public xlWorkBook As Excel.Workbook
    Public xlSheet As Excel.Worksheet
    Dim strDate$
    
    Public Sub GenerateReport()
      On Error GoTo ErrHandler
      
      ' Creating Object for Excel File.....
      Set xlAppTemp = New Excel.Application
      
      ' Making it Invisible and non-Interactive.....
      xlAppTemp.Visible = False
      xlAppTemp.DisplayAlerts = False
        ' Opening Template Excel File.....
      Set xlWorkBook = xlAppTemp.Workbooks.Open(App.Path & "\Book1.xls", , False)
      Set xlSheet = xlWorkBook.Sheets(1)
    
      ' Making Active to Worksheet 1.....
      xlSheet.Activate
    
      ' I am doing lot of things in it, but to provide you with example
      xlSheet.Cells(15, 1) = "This is my report 1"
    
      ' Formating Date to attach with new file name.....
      strDate = Format(Date, "yyyy-mm-dd")
      
      ' Saving excel file with new name on different folder.....
      xlWorkBook.SaveAs App.Path & "\Output" & strDate & ".xls"
    
    Cleanup:
      ' Destroying Objects.....
      Set xlSheet = Nothing
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
      Exit Sub
    ErrHandler:
    'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
    '(save happens just above if no error occurs)
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
    
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
    End Sub
    
    Private Sub Command1_Click()
      Call GenerateReport
      Beep
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2014
    Posts
    4

    Re: Save AS

    Quote Originally Posted by dglienna View Post
    This should do it:

    Code:
    Option Explicit
    
    Public xlAppTemp As Excel.Application
    Public xlWorkBook As Excel.Workbook
    Public xlSheet As Excel.Worksheet
    Dim strDate$
    
    Public Sub GenerateReport()
      On Error GoTo ErrHandler
      
      ' Creating Object for Excel File.....
      Set xlAppTemp = New Excel.Application
      
      ' Making it Invisible and non-Interactive.....
      xlAppTemp.Visible = False
      xlAppTemp.DisplayAlerts = False
        ' Opening Template Excel File.....
      Set xlWorkBook = xlAppTemp.Workbooks.Open(App.Path & "\Book1.xls", , False)
      Set xlSheet = xlWorkBook.Sheets(1)
    
      ' Making Active to Worksheet 1.....
      xlSheet.Activate
    
      ' I am doing lot of things in it, but to provide you with example
      xlSheet.Cells(15, 1) = "This is my report 1"
    
      ' Formating Date to attach with new file name.....
      strDate = Format(Date, "yyyy-mm-dd")
      
      ' Saving excel file with new name on different folder.....
      xlWorkBook.SaveAs App.Path & "\Output" & strDate & ".xls"
    
    Cleanup:
      ' Destroying Objects.....
      Set xlSheet = Nothing
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
      Exit Sub
    ErrHandler:
    'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
    '(save happens just above if no error occurs)
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
    
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
    End Sub
    
    Private Sub Command1_Click()
      Call GenerateReport
      Beep
    End Sub
    Thanks for the code but hope I'm just forgetting to do something
    Entered the code and got the below error
    Compile Error:
    Invalid inside Procedure[/COLOR]

    Private Sub mnuSaveAs_Click()
    Option Explicit

    Public xlAppTemp As Excel.Application
    Public xlWorkBook As Excel.Workbook
    Public xlSheet As Excel.Worksheet
    Dim strDate$

    Public Sub GenerateReport()
    On Error GoTo ErrHandler

    ' Creating Object for Excel File.....
    Set xlAppTemp = New Excel.Application

    ' Making it Invisible and non-Interactive.....
    xlAppTemp.Visible = False
    xlAppTemp.DisplayAlerts = False
    ' Opening Template Excel File.....
    Set xlWorkBook = xlAppTemp.Workbooks.Open(App.Path & "\Book1.xls", , False)
    Set xlSheet = xlWorkBook.Sheets(1)

    ' Making Active to Worksheet 1.....
    xlSheet.Activate

    ' I am doing lot of things in it, but to provide you with example
    xlSheet.Cells(15, 1) = "This is my report 1"

    ' Formating Date to attach with new file name.....
    strDate = Format(Date, "yyyy-mm-dd")

    ' Saving excel file with new name on different folder.....
    xlWorkBook.SaveAs App.Path & "\Output" & strDate & ".xls"

    Cleanup:
    ' Destroying Objects.....
    Set xlSheet = Nothing
    xlWorkBook.Close SaveChanges:=False
    Set xlWorkBook = Nothing
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
    xlAppTemp.Visible = True
    xlAppTemp.DisplayAlerts = True
    xlAppTemp.Quit
    Set xlAppTemp = Nothing
    Exit Sub
    ErrHandler:
    'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
    '(save happens just above if no error occurs)
    xlWorkBook.Close SaveChanges:=False
    Set xlWorkBook = Nothing

    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
    xlAppTemp.Visible = True
    xlAppTemp.DisplayAlerts = True

    xlAppTemp.Quit
    Set xlAppTemp = Nothing
    End Sub

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save AS

    I'm not sure why that Excel related code is posted? Are you trying to save an Excel worksheet? I did not see anything in the OP about that.

    The error message you are getting most likely means that you pasted that code inside a sub or function which would make it invalid

    As for the original question assuming that you are not trying to work with an Excel worksheet here then you should discard that code and write something specific to your needs.
    A Save As function would normally include a browse dialog [common dialog control] and then you would need to add code that would save the data on your form to a file or database in whatever form you like and of course you also need to add code that can read that data and populate the form with it when needed.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Aug 2014
    Posts
    4

    Re: Save AS

    Quote Originally Posted by DataMiser View Post
    I'm not sure why that Excel related code is posted? Are you trying to save an Excel worksheet? I did not see anything in the OP about that.

    The error message you are getting most likely means that you pasted that code inside a sub or function which would make it invalid

    As for the original question assuming that you are not trying to work with an Excel worksheet here then you should discard that code and write something specific to your needs.
    A Save As function would normally include a browse dialog [common dialog control] and then you would need to add code that would save the data on your form to a file or database in whatever form you like and of course you also need to add code that can read that data and populate the form with it when needed.
    You are correct not trying to work with an Excel work sheet. Having said that I can't seem to be able to find to much information about coding Save As. I've found building the File menu with Save & Save As to be rather easy but not much information on coding them. Can you point me in a direction where I would be able to build a couple of small programs using Save As to get the understanding of coding of Save and Save As.

    Thanks

    Thanks

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save AS

    Quote Originally Posted by bristown View Post
    You are correct not trying to work with an Excel work sheet. Having said that I can't seem to be able to find to much information about coding Save As. I've found building the File menu with Save & Save As to be rather easy but not much information on coding them. Can you point me in a direction where I would be able to build a couple of small programs using Save As to get the understanding of coding of Save and Save As.

    Thanks

    Thanks
    Well the only difference between save and save as is the file name selection. Save would save to the existing file should it already exist and save as would let you choose a file name/location to save to.
    Both would normally give the user a way to select the location and filename for the save but in the case of the basic save it would do so only if the file has not be loaded from disk or saved already. Save as would always give this option.

    You would use the common dialog control to allow the user to select/browse for the location/filename to save this link may help with that part http://www.vbforums.com/showthread.p...Dialog-Control
    Once you have the target location\filename then you need to actually save the data to the file. How you do this depends on what kind of file you want to use and/or how you want the data saved to that file.
    You may want to use a plain text file or a binary file. You may want to use a random access file or you may want to use a database engine. The methods you would use will vary based on what you want to do.
    For basic file operations you would use the BASIC Open statement and tell it which mode you want to use as in
    http://msdn.microsoft.com/en-us/libr...=vs.60%29.aspx
    http://www.dreamincode.net/forums/to...uential-files/

    For working with a database you would use the ADODB methods. You can search for examples on how to do this

    If you need additional help you should post some more detailed info about what you are trying to save and what kind of file you are wanting to save it to.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Save AS

    Better yet, post the code that you HAVE
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Aug 2014
    Posts
    4

    Re: Save AS

    Thanks for the responses. I have no code for my Save As or Save menus yet

    Here is some code I have in my program basically I'm entering values and them calculating them and giving them a value of 1-10. With my new menu Save As I would like to save my form with a name so I can reopen it and change the input and then recalculate the values after I make changes I would like to just save the new values. It would be like having some thing in say any MS program and using SaveAs and then adding to it and then Saving it. I think it can be saved as a Text file. Any response would be appreciated

    Thanks


    Private Sub cmdClear_Click()
    Dim I As Integer

    For I = Clas.LBound To Clas.UBound
    Clas(I).Text = ""
    Quarter(I).Text = ""
    QuarterScore(I).Text = ""
    HalfTime(I).Text = ""
    HalfScore(I).Text = ""
    ThreeTime(I).Text = ""
    ThreeScore(I).Text = ""
    'LastQuarter(I).Text = ""
    'LastScore(I).Text = ""
    FinalTime(I).Text = ""
    FinalScore(I).Text = ""
    OneScore(I).Text = ""
    TotalScore(I).Text = ""

    Next I


    End Sub

    Private Sub cmdCalculate_Click()
    Dim I As Integer, TempScore(0 To 9) As Integer, J As Integer
    J = 0
    Call Quarter_Score
    Call HalfTime_Score
    Call ThreeTime_Score
    'Call LastQuarter_Score
    Call FinalTime_Score
    Call PP_Score
    Call One_Score

    For I = Quarter.LBound To Quarter.UBound
    If Val(Quarter(I).Text) > 0 Then

    TempScore(I) = Val(QuarterScore(I).Text) + Val(HalfScore(I).Text) + _
    Val(ThreeScore(I).Text) + _
    Val(FinalScore(I).Text) + Val(Clas(I).Text)

    TotalScore(I).Text = TempScore(I)
    End If
    Next I

    End Sub

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save AS

    Here are a couple of simple examples that show how to save data from a textbox to a file with each textbox being written on a separate line and then read the data from the file into those text boxes.
    Code:
    Open Filename for output as #1 'Creates or overwrites the file specified and sets it to be written to
    Print #1, Text1.Text
    Print #1, Text2.text
    Close #1
    Code:
    Open FileName for Input as #1 ' opens the file specified for reading text
    Line Input #1,Text1.Text
    Line Input #1,Text2.Text
    Close #1
    If you want to allow them to use save as [i.e. browse for and select a filename] then you would want the use the common dialog control try a search to find an example
    Always use [code][/code] tags when posting code.

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

Featured