Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to apply Warning off message to my code

Posted on 2010-11-16
6
326 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

790 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