I've been trying to create a macro that is run via a batch file to generate a report and have been having some massively annoying issues with connecting to a SQL Server, probably due to some stupid mistake on my part.
My code to create a connection is as follows:
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
Set dbs = OpenDatabase("", False, False, connect)
Set qdf = dbs.CreateQueryDef("")
where 'connect' is a string containing connection details.
My problem is that Excel (2007) seems to have a problem with the very last line of that block, for whatever reason. I've added a watcher to the dbs variable, and it seems perfectly fine, so then why does it not like qdf?
The even stranger thing is that, if I put a 'Stop' command at the very beginning of the macro, start execution from the batch file, and then continue manually upon reaching the Stop, it works perfectly and does exactly what I'd like it to.
I've tried looking at DBEngine.Errors, and that tells me that I have the following problems:
General error: Invalid window handle
Connection not open
Again, this makes no sense to me, especially given that it works under manual execution, and Google hasn't yielded any answers.
P.S. If this is unclear, please also take a look at this question, which seems to be asking the same thing, but has no answer.
So, using the scientific method, a co-worker and I were able to figure out what the issue was, though I have no idea how it makes sense or why it would even be an issue in the first place. The layout of the macro was, in a rough sense, as follows:
Test database connection and close it
Create new workbook
Connect to database again and populate workbook from database calls
For some reason, however, the solution ended up being that VBA didn't like the fact that we were doing connection stuff both before and after creating the workbook. Changing the program flow to:
Create new workbook
Test database connection and close it
Connect to database again and populate workbook from database calls
did the trick. Again, I have no clue why this solved our problem, or if changing the order of this sequence changed something more significant within the code, but it works now! If anyone has any ideas as to why this might be, feel free to comment.
Related
I've been getting an error that I've never seen before. I keep seeing this:
Run-time error '3048':
Cannot open any more databases.
Having Googled it, it seems this happens when there are very complicated forms that have lots of lists or combo boxes that have their sources as a table/query. However, I've not changed these forms for a while now and I'm all of a sudden seeing this. Plus, my forms really aren't that complicated, usually just a single list and maybe 1 or 2 combo boxes. I just started seeing this error yesterday (2/2/22)
Almost in all cases I'm accessing the tables by using this code:
Dim rst As Recordset: Set rst = CurrentDb.OpenRecordset("table name or SQL statement")
Then I'll access data using something like, whether it be AddNew, Edit, or just looping through a recordset.
With rst
.AddNew
.Fields("name") = "Value"
End With
OR
Do Until rst.EOF
'Do something
.MoveNext
Loop
And I've already double checked that every time I open a recordset I close it
rst.Close
Set rst = Nothing
However, I keep seeing that error now. Is there some other way that I'm supposed to close these recordsets that properly closes the connection? I'm guessing opening these is causing something to linger that I'm not aware of.
the only solution I have right now is to completely restart my PC and then I can use it for a bit before I see the error again.
Thanks for your help!
EDIT: When I get that error, I can't even close everything in Access and manually open a table to look at it. I get this error message
Reserved error (-1104); there is no message for this error.
This is, sadly, a known current bug:
Access doesn't close properly. A remaining background process can only be terminated in task manager
There is no official info or remedy yet.
I also got these problems after updating Office 365 tot the latest version (221, build 14827.20158). When I close the Access application and reopen it, I immediately get the error. Then I first have to resatart my computer in order to have the application working until it reaches the max amount of conections.
When opening a database of an earlier version (.mdb format) in Microsoft Access version 2013 or later, you may encounter an error ‘Cannot open a database created with a previous version of your application.’
You can fix the error by converting the old database file format (.mdb) to the .accdb file format.
To work around this, Microsoft recommends opening the Access database (MDB) in Access 2007 or later versions, and then saving it in the file format you can open in Access version 2013 or higher.
Follow the below link to perform the steps to convert the .mdb file to .accdb file format:
https://support.microsoft.com/en-us/office/convert-a-database-to-the-accdb-file-format-098ddd31-5f84-4e89-8f44-db0cf7c11acd#
Best Regards,
Alice
I have an old Access solution that includes opening a connection and inserting some lines in a SQL database. I had to make a very small change that had nothing to do with that connection. After the change I compacted the database.
The solution works normally for me, but the person that normally runs it is getting the Run-time error I outlined in the subject. I had another user try and also got this error. It seems I am now the only one that can run this, yet I did nothing to the code or the access db as a whole that I can see explaining this.
The button they press that triggers the error:
Sets a variable as ADODB.Connection
Defines the connection string
ERROR TIME: Then it runs a function that executes a stored procedure using that connection. The error point is showing as occurring at the top of that function, when it attempts to CreateObject("ADODB.Command").
Run-time error '429': ActiveX component can't create object
Can anyone offer anything as to why this user that could work with it suddenly can't despite the same security, same machine, etc.
Sigh... Thanks for the responses. It turns out to be our security software and the fact that I moved the solution to a different location. Out of sight out of mind. I forget that I have some exemptions setup for myself, so I didn't see how it could be that. I just need to learn to check that stuff first.
When trying to add external data in excel, the data connection wizard does not load properly for some reason.
I select Data,> From Other sources, >From Data Connection Wizard>My Data source>table i want...
Then I have no options to set parameters, can only click "Finish" without any query set up.
Just Defaults to "Select * FROM XXXXX"..
Anyone have any ideas as to why this would be the case?
I have done this exact same process before, on multiple occasions, with to issue.
Something has changed to make this process not work properly.
There are big changes going on in the excel data input environment recently and all the process are being remodeled and reworked. I checked my excel 16 and it doesn't even list the option you mention (Data connection Wizard) anymore. I feel a strong push towards Data model which may not suit me or other, but all other connection methods are gradually ceasing to work.
I would guess you perhaps do not have a latest Excel version and so it just happens.
I'm having some issues with my code that I wasn't able to find anywhere.
Issue: While debugging my code, running it line by line with F8 in Excel, at some specific points excel does not wait for the application with which it's interacting to finish and proceeds to execute the rest of the code.
What is the macro: It's an a macro to automatically input information on SAP, in this case VA02. I do belive that this question is more VBA related than SAP related thus I'm posting here and not on SCN.
Where the problem occurs: The issue only happens when I'm changing the partners numbers in the Partner tab(either header or line item level). I've also saw this happens whenever I was trying to add something on the Conditions tab.
What error does it cause?: None, that's the odd part. While debugging, after I execute the line to change the Bill to Party for example ( Only change the number, no Enter or any other key is send) the code keeps running and execute the next lines. The even more off thing is that SAP seems to still be processing some of the changes but the VBA code keeps running without errors, meaning that SAP is not on the screen its showing.
So, if there's no errors what you want?: Well, while in regullar run mode it work flawleslly, this issue really makes it very hard to debug, forcing me to put a breakpoint in every... single...code...line...
What have you tried?: I've looked into Async and forced sync, no avail. Also looked in something related to Background Refresh but I guess that's only for query connections, I tried it with SAP GUI connection but also didn't worked.
Hope anyone can shed some light on this subject, maybe saw this previously. I will try to test on a peers computes but didn't had the chance yet, will post my findings after.
Thank you for reading and maybe replying =)
This is just a pice of the code as it's huge, I've noticed the problem is different parts, always the part that's inputing info on SAP. My peer write similar codes, that do the same thing on the same fields and he never got this erro so I think it might be related to excel somehow
'Bill site
Connection.findById("ses[" & Sesn & "]/wnd[0]/usr/" & ref & "/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/tblSAPLV09CGV_TC_PARTNER_OVERVIEW/ctxtGVS_TC_DATA-REC-PARTNER[1,1]").Text = Dwks.Cells(2, "N")
For this program the erro usually start on this line, but on other ones i've seen it start on different ones. There's nothing special about this code line, i've used it many times. What comes before it is also pretty standard, just the SAP connection. I'm avoiding post the code here because I'm sure that this is not the cause of the problem, is the same sintax i've always used, with the same elements. This is something excel related.
Just a little background: I am using Access 2010 to create forms and VBA code in an Access 2003 format database. For some reason, Access 2007 format databases always corrupt on me when I make changes and save them with a particular group of objects, but that's for another discussion.
When writing VBA code in this Access 2003 database, any time my code breaks (via breakpoint or an unhandled error) and I make a correction, Access tells me that it can't save back to the database because another user has it open. However, I am the only user working on the database; this is a local copy of the database and it's sitting on my desktop.
The LDB file can't be deleted because Access is using it. When I first load the database, I see my machine name and "Admin" when opening the LDB in a text or hex editor. After a break, I see that plus a duplicate entry, but this time around "admin" has a lower-case "A."
Closing the database and reopening it fixes the problem but makes it needlessly cumbersome to debug my code. Anyone else encounter this issue and/or have a fix for it?
It might be helpful to know what your code is doing when this happens. Certainly that's not normal behavior. For instance, are you opening another database with New Access.Application? Are you using ADO or DAO to access records in the database with a connection string?
There are no external connections to the database at all.
It may not matter if there are external connections to the database if you are using a connection string to connect to the open database; not sure but that may be seen as an external connection... you may want to use CurrentDB for DAO, or CurrentProject.Connection as your ActiveConnection for any ADO queries.
I am assuming that this problem persists through reboots; but for the sake of argument, try closing out Access and going to the task manager to make sure you have no other instances of MSAccess.exe running. You might even try closing all Office products and/or making sure that Access is the only Office product running. I have seen some weird conflicts between Microsoft Communicator and Outlook; so it's not entirely out of the question for Access to have issues with another MS product.
You may also want to check the size of the database to make sure it's not exceeded 2GB. That causes the infamous "Invalid parameter" error; perhaps it might be causing this as well.
With no other details about how your program works, we may only be able to offer generic advice like this.
I have discovered a way to cause the problem discussed above (and thereby to correct it). Turns out if you create a database object and set it to the current database, you get this problem.
That is,
dim cdb as database
set cdb = currentdb
From this point on, you're cooked.
Instead, figure a way around this by possibly using currentdb directly or not using it at all.
This worked for me.
In your VBA Try checking that all your open Connections to the database are closed. Until the connection is open the LDB fill will be there.
Same symptom of not being able to save form or code mods after application had started. I found a workaround today! In the startup of my first form of the app, I had issued a "DAO.DBEngine.SetOption dbMaxLocksPerFile, 20000". Commenting this statement removed the problem. I did no further testing, but FYI, the DBEngine call was before any reference or attempt to use CurrentDB(). Also the current default on my Access 16 install is 9,500.
I thought I might answer here, since I stumbled upon this question while having a similar issue. Essentially, it boiled down to this: I could either edit forms, VBA, etc. or edit information in the local database (which I'm using as a cache) with currentDB. I also have a backend database, but the locking was clearly on the frontend database.
The solution ended up being weird, but stupidly simple. When the frontend starts up, I have it immediately create a connection to the backend using OpenRecordset (and similarly to you, that backend was still on my own computer for testing purposes). I tried temporarily disabling that code, and suddenly it wasn't an issue anymore. And it turns out, once I call currentDB, I can then call OpenRecordset to open the connection to the backend, and suddenly it isn't a problem anymore.
Tl;Dr: if you're calling OpenRecordset somewhere in your code to connect to a backend, be sure to call something like set db = currentDB beforehand, then everything works. (That is, probably until I publish this answer and Access then decides it doesn't want to anymore).
Why this fixed it is beyond me, someone with more knowledge can maybe answer that.
The solution:
options > current database > click enable -track name auto correct info