简体   繁体   English

MS Access VBA问题

[英]MS Access VBA issue

I'm making a report in MS Access - what I'm trying to do here is basically APPEND a query to a table that I've already created - I select the first value, change it and update the table. 我在MS Access中创建一个报告 - 我在这里尝试做的基本上是对我已经创建的表的APPEND查询 - 我选择第一个值,更改它并更新表。 The issue that I'm coming across is - this report will be used by a VB6 application. 我遇到的问题是 - 这个报告将由VB6应用程序使用。 So the user won't be seeing Access at all. 因此用户根本不会看到Access。

The thing with my append query is that it needs a USER ID to run (4 digit number). 我追加查询的问题是它需要运行一个USER ID (4位数字)。 Normally when I run a report in Access I pass the parameters to a form in Access - and I use them to run queries. 通常,当我在Access中运行报表时,我会将参数传递给Access中的表单-然后使用它们来运行查询。 However, in this case, I need the user to enter a value when appending the query, additionally, when appending a query in VBA it first says "You are about to append a query, are you sure" (or something along those lines), so is there a way to automate that as well, so when I append it nothing happens? 但是,在这种情况下,我需要用户在附加查询时输入一个值,另外,当在VBA中附加查询时,它首先说"You are about to append a query, are you sure" (或者沿着那些行) ,有没有办法自动化,所以当我追加它没有任何反应?

Here is my code for appending and selecting date from the tempTable : 这是我从tempTable添加和选择日期的tempTable

CurrentDb.Execute "DELETE from [tempCompanyMgmt-NOW];"
DoCmd.OpenQuery "qryCompanyMgmt-SUE" - i made this append!
Set rs1 = CurrentDb.OpenRecordset("Select * from [tempCompanyMgmt-NOW]", , dbOpenDynamic)

So as long as I press OK, YES when I get notified of the APPEND process and enter the parameter for USER ID - everything works fine. 因此,只要我按OK,当我收到APPEND流程通知并输入USER ID参数时, USER ID一切正常。

Looks like a typo in your markdown, should the 2nd line be: 第二行应该是您的减价中的错字。

DoCmd.OpenQuery "qryCompanyMgmt-SUE - i made this append!"

You'll need to remove the reference to the form inside the qryCompanyMgmt-SUE - i made this append! 你需要删除qryCompanyMgmt-SUE - i made this append!对表单的引用qryCompanyMgmt-SUE - i made this append! query, and swap it for a parameter name. 查询,并将其替换为参数名称。 You can use the Access interface to explicitly add a parameters clause to the query, and then using ADO (or DAO) from VB6, set a parameter value before you open/execute the query. 您可以使用Access接口向查询显式添加parameters子句,然后使用VB6中的ADO(或DAO),在打开/执行查询之前设置参数值。

The "You are about to append a query, are you sure" message is an Access feature (and it can be disabled), so if you want the VB6 application to provide such a warning, then you'll need to create it yourself with a MsgBox . "You are about to append a query, are you sure"消息是一种访问功能(可以禁用),因此,如果您希望VB6应用程序提供这样的警告,则需要您自己创建一个MsgBox

One option would by putting your append query into the code and filling in the parameter that way. 一种选择是将您的追加查询放入代码中,然后以这种方式填写参数。

I don't know your exact scenario, but something like: 我不知道您的确切情况,但是类似:

If not isValidUserID(me.UserID) Then
    msgbox "Please enter a a valid user id"
    exit sub
End If



Dim strSQL As String
strSQL = "DELETE * from [tempCompanyMgmt-NOW];"
CurrentDb.Execute strSQL, dbFailOnError


strSQL = "INSERT INTO tempCompanyMgmt-NOW ( FieldName1, FieldName2, FieldName3 ) " & _
        "SELECT FieldName1, FieldName2, FieldName3 FROM tempCompanyMgmt WHERE UseriD=" & Me.UserID

CurrentDb.Execute strSQL, dbFailOnError

To validate the user id you could do something like: 要验证用户标识,您可以执行以下操作:

If (Len(me.UserID) = 4 And IsNumeric(me.UserID)) Then

or 要么

Public Function isValidUserID(varUserID As Variant) As Boolean
Dim blnRet As Boolean

If Len(varUserID) = 4 And IsNumeric(varUserID) Then
    blnRet = True
End If

isValidUserID = blnRet
End Function

To get rid of the MsgBox telling me I'm about to append a query i included this in my module before I open my append query.. 为了摆脱MsgBox告诉我,我打算附加一个查询,我在打开我的追加查询之前将其包含在我的模块中。

DoCmd.SetWarnings False

And I realized once I have the value passed to the form (userID), that value gets passed on as a parameter when my query gets appended. 我意识到,一旦我将值传递给表单(userID),当我的查询被追加时,该值将作为参数传递。 So it's all set. 一切准备就绪。 Thanks for all help! 谢谢大家的帮助!

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

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