开发者

Passing a form and list box

I'm trying to develop a sub that I can pass different variables to save me some time when creating quite a few different types of listbox in VBA Access. I've come up with the following:

Public Sub openCASEFORM(f As Form, list As ListBox)
DoCmd.OpenForm f, , , "[FileID]=" & f.list
End Sub

And I call it as follows:

Private Sub listPreAn_Click()
Call openCASEFORM(Me, listPreAn)
End Sub

However when trying it I get a Run-time error '2465' Application-defined 开发者_如何学Cor object-defined error. I'm struggling to see where it's going wrong. Any thoughts?


What happens if you use Call openCaseFORM(Me, Me!listPreAn ?

Well of if you do not use list as Listbox as second parameter, You are not using it anyway.


Your code is really badly written, as you're passing objects but you need to use strings to do the job. I'd rewrite it thus:

  Public Sub openCASEFORM(f As String, list As String)
    DoCmd.OpenForm f, , , "[FileID]=" & f(list)
  End Sub

Either that, or:

  Public Sub openCASEFORM(f As String, list As ListBOx)
    DoCmd.OpenForm f, , , "[FileID]=" & list.Value
  End Sub

Both of these assume the listbox is a simple one (i.e., not multiselect).

I would suggest that it would make more sense to rewrite thus:

  Public Sub openCASEFORM(ByVal strFormName As String, ByVal strCriteria As String)
    DoCmd.OpenForm strFormName, , , strCriteria
  End Sub

That way, you could pass any arbitrary WHERE clause you liked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜