[英]MS Access: Choosing Worksheet from Excel Workbook
I'm writing some code in MS Access and I reached to the point where user needs to choose on which worksheet of an Excel workbook there need to be performed some operation.我正在 MS Access 中编写一些代码,并且达到了用户需要选择需要在 Excel 工作簿的哪个工作表上执行某些操作的地步。 I don't know, what name of this worksheet is or on which position it is placed.
我不知道,这个工作表叫什么名字,或者放在哪个位置。
I was thinking about a solution which will show user a form (as modal form) with listbox containing all sheets names'.我正在考虑一个解决方案,它将向用户显示一个包含所有工作表名称的列表框的表单(作为模态表单)。 When user click one of them form will show aside A1:J10 range (so user can choose the right one worksheet).
当用户单击其中一个表单时,将显示 A1:J10 范围(因此用户可以选择正确的一张工作表)。 After confirming choosen worksheet it will return as worksheet object.
确认选择的工作表后,它将作为工作表对象返回。
Every thing was great untill I wanted to pass a object variable to the form.每件事都很棒,直到我想将一个对象变量传递给表单。 In openArgs I can only pass a string.
在 openArgs 中,我只能传递一个字符串。 I was even thinking about a class which will open this form but it's still no luck with passing object parameter.
我什至在考虑一个可以打开这个表单的类,但是传递对象参数仍然没有运气。
I'm trying to avoid global/public variables.我试图避免全局/公共变量。
Any ideas?有任何想法吗?
Assuming your object is wsObj, can't you just use wsObj.假设您的对象是 wsObj,您不能只使用 wsObj。
Name
? Name
?
Also have a look at wsObj.也看看 wsObj。
CodeName
, which may be interesting as well. CodeName
,这可能也很有趣。
IN the forms Module you can declare a property as object and then set that property once loaded.在表单模块中,您可以将属性声明为对象,然后在加载后设置该属性。 So in the form module
所以在表单模块中
Option Explicit
Private myObj as object
Property Set DesiredWorksheet(o as object)
set myobj = o
End
and then in your code然后在你的代码中
Load myform
set myform.desiredworksheet = wsObj
myform.show
Ahh, sorry I was writing Excel not Access!!!啊,抱歉我写的是 Excel 而不是 Access!!!
Docmd.openform f
f.desiredworksheet = ws.obj
docmd.openform f, windowmode:=acdialog
ought to work应该工作
There are many possibilities to send some value between objects.在对象之间发送一些值有很多可能性。
A ) Using Global vars into ACCESS Vba module A ) 使用全局变量进入 ACCESS Vba 模块
Global yourvariable As String
if you need some different value can use Variant, Single, etc.如果您需要一些不同的值,可以使用 Variant、Single 等。
B ) Using Windows Register To save value: B ) 使用 Windows 寄存器来保存值:
SaveSetting "yourprojectname", "Settings", "yourvariable", yourvalue
To retrieve value:检索值:
retvalue = GetSetting("yourprojectname", "Settings", "yourvariable", "your_default_value_if_not_exist")
C ) Using OpenArg into Open Form command procedure C ) 使用 OpenArg 进入 Open Form 命令程序
DoCmd.OpenForm "stDocName", acNormal, "filter_if_needed", "stlinkcriteria", acFormEdit, acWindowNormal, "arguments_forOpenArgs"
On destination form在目的地表格上
Private Sub Form_Open(cancel as integer)
your_args=Me.OpenArgs
On all three possible solutions you can send more than one value using a chain with vars and values, an example:在所有三种可能的解决方案中,您可以使用带有变量和值的链发送多个值,例如:
myvar_mutiple="name=John Doe|address=Rua del Percebe 34|location=Madrid|phone=34 91 1234567"
On this example i used "pipe" (AltGr on key 1) char to separate each var=value Then on destination procedure only need split each pair:在这个例子中,我使用“管道”(键 1 上的 AltGr)字符来分隔每个 var=value 然后在目标过程中只需要拆分每一对:
splitvar=Split(myvar_multiple,"|")
With this you obtain for each "splitvar" an element like "name=John Doe" Do again an split with "=" to obtain variable an value.有了这个,你可以为每个“splitvar”获得一个像“name=John Doe”这样的元素,再次用“=”分割以获得变量一个值。 For each value you can reassign the result to a local vars.
对于每个值,您可以将结果重新分配给本地变量。 Full code example:
完整代码示例:
if me.OpenArgs<>"" then
splitvar=Split(me.OpenArgs,"|")
for x=0 to ubound(splitvar)
tmpsplit=Split(splitvar(x),"=")
paramvars=tmpsplit(0)
paramvalue=tmpsplit(1)
select case paramvars
case "name"
stname=paramvalue
case "address"
straddress=paramvalue
case "location"
strlocation=paramvalue
case "phone"
strphone=paramvalue
end select
next
end if
Some recommendations that i use for this code "multiple vars": - always use Low Case variable or change this:我用于此代码“多个变量”的一些建议: - 始终使用小写变量或更改此:
paramvars=tmpsplit(0)
by经过
paramvars=lcase(tmpsplit(0))
-if you need to use "=" into value you can change by other alternative char or search the first "=" form left (i used this solution instead Split) - 如果您需要使用“=”转换为值,您可以通过其他替代字符进行更改或搜索左侧的第一个“=”表单(我使用此解决方案而不是拆分)
paramvars=trim(lcase(left(splitvar(x),len(splitvar(x))-(len(splitvar(x))-instr(splitvar(x),"="))-1)))
Over your solution to select Sheet on excel from Access i think there are better alternatives.关于从 Access 中选择 Sheet on excel 的解决方案,我认为有更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.