Got a quick question about error handling in VBA, specifically pertaining to a module that exports results from Access queries, into an Excel file. My current module is working fine for the most part, but during negative testing, I've found a few specific places where the customer would receive an error message.
When the user clicks the Export button, they are prompted to choose a file name and file path, and then click Save (it's just the default File -> Save page within Microsoft, nothing I've made myself).
The first error case is when the user clicks my Export button, and then clicks "Cancel" on the save menu. I receive a run-time error that says "The OutputTo action was cancelled". Pretty self-explanatory, but I'm hoping to make a message box that is more user-friendly than the default run-time error box.
The second error case is when the user tries to save the Excel file, and there already exists a file with the same name in the same path AND it's already open. If it's not open, you're just asked if you want to overwrite it like normal. But if it's open, you get a different run-time error. Through searching, I've found solutions for this, but they are only checking static hard-coded file names/paths. The challenge for me is that the customer would choose this information, and it would be different each time.
In my head, I'm envisioning two basic if statements:
If *user clicks Cancel* Then
MsgBox "Export cancelled."
Exit Sub
End If
If *file name already exists* And *that file is open* Then
MsgBox "There is already an Excel file with this name. Please close that Excel file before continuing."
Exit Sub
End If
The problem is that I don't know the actual code for those 3 pseudo-code statements I put in asterisks. Is there a simple statement I could put in each of those places? I'm hoping this doesn't involve dozens and dozens of lines of code. I'm also hoping to avoid On Error statements if possible, because I still haven't quite wrapped my head around them. Using that approach, I've gotten message boxes to display, but it's just one generic catch-all error message. I would be nervous that another run-time error would occur during my testing, and I wouldn't know about it and which specific error it was.
If you have any suggestions for me, I would greatly appreciate them! Thanks so much everyone!!!!
When the user clicks the Export button, they are prompted to choose a file name and file path, and then click Save (it's just the default File -> Save page within Microsoft, nothing I've made myself).
The first error case is when the user clicks my Export button, and then clicks "Cancel" on the save menu. I receive a run-time error that says "The OutputTo action was cancelled". Pretty self-explanatory, but I'm hoping to make a message box that is more user-friendly than the default run-time error box.
The second error case is when the user tries to save the Excel file, and there already exists a file with the same name in the same path AND it's already open. If it's not open, you're just asked if you want to overwrite it like normal. But if it's open, you get a different run-time error. Through searching, I've found solutions for this, but they are only checking static hard-coded file names/paths. The challenge for me is that the customer would choose this information, and it would be different each time.
In my head, I'm envisioning two basic if statements:
If *user clicks Cancel* Then
MsgBox "Export cancelled."
Exit Sub
End If
If *file name already exists* And *that file is open* Then
MsgBox "There is already an Excel file with this name. Please close that Excel file before continuing."
Exit Sub
End If
The problem is that I don't know the actual code for those 3 pseudo-code statements I put in asterisks. Is there a simple statement I could put in each of those places? I'm hoping this doesn't involve dozens and dozens of lines of code. I'm also hoping to avoid On Error statements if possible, because I still haven't quite wrapped my head around them. Using that approach, I've gotten message boxes to display, but it's just one generic catch-all error message. I would be nervous that another run-time error would occur during my testing, and I wouldn't know about it and which specific error it was.
If you have any suggestions for me, I would greatly appreciate them! Thanks so much everyone!!!!