Solved

How to apply Warning off message to my code

Posted on 2010-11-16
6
322 Views
Last Modified: 2012-06-27
I have a code below.  It works, but when I try to 1) check if csv file has appropriate column title and 2) if not, update the titles, and 3) save it into xls file, the warning message pops up, saying "A file named S:\db\invoice.xls exists.  Do you want to replace it?  I click "Yes" anyway, so how can I turn off the warning?  Thank you very much in advance.  -- hb

My code is below:

Private Sub ImportCSV_Click()

    Dim xl As Object, strFilePath As String
    strFilePath = "S:\Db\invoice.csv"
    Set xl = CreateObject("Excel.Application")
    ' make the excel application visible
    xl.Visible = True
    ' open the workbook
   
    xl.Workbooks.Open strFilePath
     If xl.Range("A1").Value = "InvoiceNo" Then
         'do nothing
         Else
    xl.Range("1:1").insert
    xl.ActiveSheet.Range("A1").Value = "InvoiceNo"
    xl.ActiveSheet.Range("B1").Value = "Amount"
    xl.ActiveSheet.Range("C1").Value = "DueDate"

    ' save and close the file
    If Val(xl.Version) < 12 Then
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls"
    Else
        ' to use XLSX format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsx", 51
        ' or to use XLSM format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsm", 52
        ' or to use XLSB format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsb", 50
        ' or to use good old XLS format:
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls", 56
    End If
   
    xl.Workbooks(1).Close SaveChanges:=True
    ' close Excel
   
    End If
    xl.Quit
    'clean up
    Set xl = Nothing    
             
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Invoice", "S:\Db\invoice.xls", True
     
    MsgBox "Completed"

End Sub
0
Comment
Question by:kg6lfz
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:broomee9
ID: 34148875
Re-posting as expert comment:

To turn off warnings in Excel, do this:
Application.DisplayAlerts = False

Turn it back on like this:
Application.DisplayAlerts = True

In Acces it would be this to turn them off
DoCmd.SetWarnings False

And this to turn them on:
DoCmd.SetWarnings True
0
 
LVL 24

Expert Comment

by:broomee9
ID: 34148894
And for your specific example, it would be this:

xl.DisplayAlerts = False

Since you define the application as xl in your code.
0
 

Author Comment

by:kg6lfz
ID: 34149781
Thank you, broomee9 -
I am sorry to ask, but based on my code, where should I put the "x1.DisplaiyAlerts = False" and "x1.DisplaiyAlerts = True".  Sorry for being novice.  Thank you.
0
 
LVL 24

Accepted Solution

by:
broomee9 earned 300 total points
ID: 34149971
No worries.

You want to put it at the beginning and then again at the end to turn it back on.
Private Sub ImportCSV_Click()

    Dim xl As Object, strFilePath As String
    
    strFilePath = "S:\Db\invoice.csv"
    Set xl = CreateObject("Excel.Application")
    
    'Turn off warnings
    xl.DisplayAlerts = False
    
    ' make the excel application visible
    xl.Visible = True
    ' open the workbook
   
    xl.Workbooks.Open strFilePath
     If xl.Range("A1").Value = "InvoiceNo" Then
         'do nothing
         Else
    xl.Range("1:1").insert
    xl.ActiveSheet.Range("A1").Value = "InvoiceNo"
    xl.ActiveSheet.Range("B1").Value = "Amount"
    xl.ActiveSheet.Range("C1").Value = "DueDate"

    ' save and close the file
    If Val(xl.Version) < 12 Then
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls"
    Else
        ' to use XLSX format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsx", 51
        ' or to use XLSM format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsm", 52
        ' or to use XLSB format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsb", 50
        ' or to use good old XLS format:
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls", 56
    End If
   
    xl.Workbooks(1).Close SaveChanges:=True
    ' close Excel
   
    End If
    
    xl.Quit
    
    'Turn on warnings
    xl.DisplayAlerts = False
    'clean up
    Set xl = Nothing    
             
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Invoice", "S:\Db\invoice.xls", True
     
    MsgBox "Completed"

End Sub

Open in new window

0
 

Author Comment

by:kg6lfz
ID: 34150466
It worked perfectly.  Thank you very much for your support.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now