NewAS
asked on
Unbound form with unbound subform.
I have an unbound form which only displays certain data from a table depending on what data priviledges the user has. Now I wish to include a subform which will show data that matches a field on the unbound form.
The reason I need to do it this way is for data editing/entry purposes. I need to amend the data on the unbound form but not the subform.
The code i use on the On Load event of the unbound form called frmRelatedContactsAilsaFor ums is as follows:
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I D FROM tblContacts WHERE (((tblContacts.contact_typ e_ID)=5 Or (tblContacts.contact_type_ ID)=6 Or (tblContacts.contact_type_ ID)=7 Or (tblContacts.contact_type_ ID)=24))", cnn
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
End Sub
The code i use on the On Load event of the unbound subform called frmRelatedContactsAilsaFor umsSubAddr ess is as follows:
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA ilsaForums ]![Post_Co de] & "'", cnn
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
End Sub
I am having issue with the following syntax:
Forms![frmRelatedContactsA ilsaForums ]![Post_Co de]
If I open the unbound form frmRelatedContactsAilsaFor um on its own without a subform then open the subform frmRelatedContactsAilsaFor umSubAddre ss it works, but if I put the subform back into the form frmRelatedContactsAilsaFor um I get the following error:
Run-time error '2424':
The expression you entered as a field, control, or property name that Domestic - Habia can't find.
Any help would be much appreciated, NewAS.
The reason I need to do it this way is for data editing/entry purposes. I need to amend the data on the unbound form but not the subform.
The code i use on the On Load event of the unbound form called frmRelatedContactsAilsaFor
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
End Sub
The code i use on the On Load event of the unbound subform called frmRelatedContactsAilsaFor
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
End Sub
I am having issue with the following syntax:
Forms![frmRelatedContactsA
If I open the unbound form frmRelatedContactsAilsaFor
Run-time error '2424':
The expression you entered as a field, control, or property name that Domestic - Habia can't find.
Any help would be much appreciated, NewAS.
try this
move your code from the subform's form_load into a public procedure within that form
then main form's form_load calls this public procedure
move your code from the subform's form_load into a public procedure within that form
then main form's form_load calls this public procedure
you can move these codes
"SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA ilsaForums ]![Post_Co de] & "'", cnn
in the load event of the main form and set the recordsource of the subform
"SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA
in the load event of the main form and set the recordsource of the subform
ASKER
I like the thought of the adding the subform code to the main form, could you look at my code and tell me the best way to have two database connections and how i reference the second to the subform?
Regards, NewAS.
Regards, NewAS.
try this codes in the load event of the main form
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I D FROM tblContacts WHERE (((tblContacts.contact_typ e_ID)=5 Or (tblContacts.contact_type_ ID)=6 Or (tblContacts.contact_type_ ID)=7 Or (tblContacts.contact_type_ ID)=24))", cnn
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA ilsaForums ]![Post_Co de] & "'", cnn2
End With
'try either of these two lines
Set Me.frmRelatedContactsAilsa ForumsSubA ddress.Rec ordset = rst2
' Set Me.frmRelatedContactsAilsa ForumsSubA ddress.For m.Recordse t = rst2
Set rst2 = Nothing
Set cnn2 = Nothing
end sub
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsA
End With
'try either of these two lines
Set Me.frmRelatedContactsAilsa
' Set Me.frmRelatedContactsAilsa
Set rst2 = Nothing
Set cnn2 = Nothing
end sub
ASKER
Nearly there, but when I navigate to another record using the standard record navigation buttons, the subform record does not update, could you please advise?
The above code worked with Set Me.frmRelatedContactsAilsa ForumsSubA ddress.For m.Recordse t = rst2
Regards, NewAS.
The above code worked with Set Me.frmRelatedContactsAilsa
Regards, NewAS.
ASKER
Could someone please advise as to how to get the rst2 recordset to refresh when using the main form record navigation buttons.
did you set the links master/child fields ?
ASKER
I didnt think you could use master/child fields with unbound forms, am i wrong?
hmm??
can you zip and upload your db here
http://www.ee-stuff.com
remove sensitive data and workgroup if there are any.
can you zip and upload your db here
http://www.ee-stuff.com
remove sensitive data and workgroup if there are any.
ASKER
I would prefer not to, could you please advise around this?
try in the main form current event
Private Sub Form_Current()
Me.frmRelatedContactsAilsa ForumsSubA ddress.req uery
' Me.frmRelatedContactsAilsa ForumsSubA ddress.For m.requery
End Sub
Private Sub Form_Current()
Me.frmRelatedContactsAilsa
' Me.frmRelatedContactsAilsa
End Sub
you can try this too
Set Me.frmRelatedContactsAilsa ForumsSubA ddress.For m.Recordse t = rst2
Me.frmRelatedContactsAilsa ForumsSubA ddress.Lin kChildFiel ds = "Post_Code"
Me.frmRelatedContactsAilsa ForumsSubA ddress.Lin kMasterFie lds = "Post_Code"
Set Me.frmRelatedContactsAilsa
Me.frmRelatedContactsAilsa
Me.frmRelatedContactsAilsa
ASKER
Tried both with no joy. I have a button that runs a similar query to filter the main dataset even further:
Private Sub btnEmploymentLaw_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I D FROM tblContacts WHERE (((tblContacts.contact_typ e_ID)=24)) ", cnn
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE mmaClubMem bers]![Pos t_Code] & "'", cnn2
End With
Set Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Reco rdset = rst2
Set rst2 = Nothing
Set cnn2 = Nothing
End Sub
This works well for the first record, but as soon as you navigate to the next record the subform will not update, this is driving me mad.
Private Sub btnEmploymentLaw_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.ConnectionString = "DSN=HabiaLocal"
cnn.Open
With rst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblContacts.contacts_ID, tblContacts.Post_Code, tblContacts.Real_name, tblContacts.Surname, tblContacts.title_ID, tblContacts.Occupation, tblContacts.Organisation, tblContacts.Tel_No, tblContacts.Tel_No2, tblContacts.Mobile, tblContacts.Fax_No, tblContacts.Author_email, tblContacts.Homepage, tblContacts.contact_type_I
End With
Set Me.Recordset = rst
Set rst = Nothing
Set cnn = Nothing
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE
End With
Set Me.frmRelatedContactsEmmaC
Set rst2 = Nothing
Set cnn2 = Nothing
End Sub
This works well for the first record, but as soon as you navigate to the next record the subform will not update, this is driving me mad.
in design view of the main form
select the outer box of the subform hit F4
In the Data tab , what is listed in the Source Object ?
is it the same name as the subform { frmRelatedContactsAilsaFor umsSubAddr ess } ?
if not, post the name of the Source Object.
btw, do you have another subform in that main form {frmRelatedContactsEmmaClu bMembersSu bAddress } ?
select the outer box of the subform hit F4
In the Data tab , what is listed in the Source Object ?
is it the same name as the subform { frmRelatedContactsAilsaFor
if not, post the name of the Source Object.
btw, do you have another subform in that main form {frmRelatedContactsEmmaClu
ASKER
If i put Set Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Reco rdset = rst2 on the On Current event I get an Object Required error which I think is due to the On Load event which actually defines the recordset.
The dont work either:
Me.frmRelatedContactsEmmaC lubMembers SubAddress .LinkChild Fields = "Post_Code"
Me.frmRelatedContactsEmmaC lubMembers SubAddress .LinkMaste rFields = "Post_Code"
I get Run-time error '31':
Data provider could not be initialised.
Any thoughts?
Probably because Post_Code is not
The dont work either:
Me.frmRelatedContactsEmmaC
Me.frmRelatedContactsEmmaC
I get Run-time error '31':
Data provider could not be initialised.
Any thoughts?
Probably because Post_Code is not
ASKER
I changed the name of the form frmRelatedContactsAilsaFor umsSubAddr ess to frmRelatedContactsEmmaClub MembersSub Address, sorry for the confusion.
what about the other questions?
ASKER
The source object is frmRelatedContactsEmmaClub MembersSub Address.
ASKER
We seemed to cross over a little with our messages, have I answered your questions properly?
try this
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE mmaClubMem bers]![Pos t_Code] & "'", cnn2
End With
'insert the following remove later
rst2.movelast
msgbox rst2.recordcount
'-------
Set Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Reco rdset = rst2
post here the content of the message box
is it 1 ?
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE
End With
'insert the following remove later
rst2.movelast
msgbox rst2.recordcount
'-------
Set Me.frmRelatedContactsEmmaC
post here the content of the message box
is it 1 ?
ASKER
Yes it is 1.
so, don't expect it to move to the next record, coz there is no next record.
ASKER
No what i mean is that the main form frmRelatedContactsEmmaClub Members recordset rst has 665 records and i am navigating these records, I want the subform frmRelatedContactsEmmaClub MembersSub Address to refresh everytime I move to another record on the form frmRelatedContactsEmmaClub Members.
Sorry if I was not clear.
Sorry if I was not clear.
then place this in the current event of the main form
or place these codes in a sub and call the sub in the current event of the main form
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE mmaClubMem bers]![Pos t_Code] & "'", cnn2
End With
Set Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Reco rdset = rst2
Set rst2 = Nothing
Set cnn2 = Nothing
or place these codes in a sub and call the sub in the current event of the main form
Dim cnn2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
cnn2.ConnectionString = "DSN=HabiaLocal"
cnn2.Open
With rst2
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT tblMember.Address_1, tblMember.Address_2, tblMember.Address_3, tblMember.City, tblMember.County, tblMember.Location, tblMember.Post_Code FROM tblMember WHERE tblMember.Post_Code='" & Forms![frmRelatedContactsE
End With
Set Me.frmRelatedContactsEmmaC
Set rst2 = Nothing
Set cnn2 = Nothing
ASKER
Ok tried that and it just crashed my database. I also tried removing the above code from the On Load event in case or possible duplication of code and it still crashed, it doesnt like being in the Current event.
lets use the the codes in the load event of the main form,
but remove the Where Clause for rst2
let's try to filter the recordset in the subform in the current event of the main form
with
Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Filt er="[Post_ Code]='" & me.[Post_code] &"'
Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Filt erOn=True
Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Refr esh
but remove the Where Clause for rst2
let's try to filter the recordset in the subform in the current event of the main form
with
Me.frmRelatedContactsEmmaC
Me.frmRelatedContactsEmmaC
Me.frmRelatedContactsEmmaC
ASKER
That sort of worked but I more often than not get a Run-time error '31' error Data Provider could not be initialized.
Any thoughts, NewAS.
Any thoughts, NewAS.
ASKER
The code is having issue with:
Me.frmRelatedContactsEmmaC lubMembers SubAddress .Form.Filt er = "[Post_Code]='" & Me.[Post_Code] & "'"
Regards, NewAS.
Me.frmRelatedContactsEmmaC
Regards, NewAS.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Genius, genius, you are my Hero, just kidding. Spot on, works a treat, thank you so much for your patience and knowledge.
Thank you, NewAS.
Thank you, NewAS.
ASKER
Oops, spoke to soon.
I opened the form again, now its throwing up the same error. This is so weird.
I opened the form again, now its throwing up the same error. This is so weird.
thus the reference to the Main form {Forms![frmRelatedContacts