简体   繁体   English

SQL Server ODBC连接问题

[英]SQL Server ODBC connection issue

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. 我一直在尝试创建一个通过批处理文件运行以生成报告的宏,并且在连接到SQL Server时遇到了一些烦人的问题,这可能是由于我的一些愚蠢错误所致。

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. 其中“ connect”是包含连接详细信息的字符串。

My problem is that Excel (2007) seems to have a problem with the very last line of that block, for whatever reason. 我的问题是,无论出于何种原因,Excel(2007)似乎都对该块的最后一行有问题。 I've added a watcher to the dbs variable, and it seems perfectly fine, so then why does it not like qdf? 我在dbs变量中添加了一个监视程序,它看起来非常好,那么为什么它不喜欢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. 甚至更奇怪的是,如果我在宏的最开始放置了“ Stop”命令,则从批处理文件开始执行,然后在到达Stop位置时手动继续执行,它可以完美地工作并完全满足我的要求它来。

I've tried looking at DBEngine.Errors, and that tells me that I have the following problems: 我尝试查看DBEngine.Errors,这告诉我存在以下问题:

  • 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. 再说一次,这对我来说毫无意义,特别是考虑到它可以手动执行,而且Google尚未给出任何答案。

PS If this is unclear, please also take a look at this question , which seems to be asking the same thing, but has no answer. PS:如果不清楚,也请看一下这个问题 ,似乎是在问同样的问题,但没有答案。

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. 但是由于某种原因,解决方案最终是VBA不喜欢我们在创建工作簿之前和之后都在做连接工作的事实。 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. 如果有人对为什么会这样有任何想法,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM