Bind Form to XML source

  • Thread starter PANCZO via AccessMonster.com
  • Start date
P

PANCZO via AccessMonster.com

HI!

I have a problem...
I try to bind XML data to (unbound) form
so, on Load:

Dim rst As New ADODB.Recordset
On Error Resume Next
rst.Open "pathtomyxmlfile", "Provider=MSPersist;", , , adCmdFile

If Err.Number = 0 Then
Set me.recordset = rst
End If


And I see data on the form (I cut code with sets controlsource)
, but I can't modyfy and add new records.
Any solutions?
 
P

Pat Hartman

XML isn't a direct access file. It is a flat file. It would be treated as
a .txt file would be treated and you can't update those either.
 
B

Brendan Reynolds

If this is Access 2000, that's a limitation of the Recordset property in
Access 2000, the form will always be read-only. In later versions of Access
the form can be read/write, but you must set the CursorLocation, CursorType
and LockType properties of the recordset - as always when opening an ADO
recordset, if you accept the default values for these properties you get a
read-only, forward-only recordset. Here's an example ...

Option Compare Database
Option Explicit

Dim mrst As ADODB.Recordset

Private Sub cmdLoad_Click()

Set mrst = New ADODB.Recordset
With mrst
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open CurrentProject.Path & "\test.xml", , , , adCmdFile
End With

Set Me.Recordset = mrst

End Sub

Private Sub cmdSave_Click()

mrst.Save

End Sub

Private Sub Form_Close()

If Not mrst Is Nothing Then
If mrst.State <> adStateClosed Then
mrst.Close
End If
End If

End Sub
 
P

PANCZO via AccessMonster.com

Thanks Bernard!

I can modify, but I still "fight" with add new record on the form...
It is possible that me idea is not got, but I write application (as .adp)
in Access 2003, and this solutions I think is neccessery.
I try explain:
So, when user want for example edit invoice, I connect to SQL Server get the
data and give user posibility to edit. Simple...
But sometimes the invoice have a ca. 1000 lines, and user make this a few
houres. and somtimes power faliure or connection lose, so I decied to give
him local source. So, when users get invoice I seve recordset to xml, and
wbind data from file to form, and time to taime, app save the updated
recordset to xml.
 
B

Brendan Reynolds

It's Brendan, not Bernard.

This seems to work ...

Private Sub cmdNew_Click()

Me.Recordset.AddNew

End Sub

I can't comment on whether this is a good approach or not, an invoice with
1000 items is something outside of my experience.
 
P

PANCZO via AccessMonster.com

Tahnks for all!

Full code from my "testform"
=================================================================
Option Compare Database
Option Explicit

Dim mrst As ADODB.Recordset

Private Sub cmdSave_Click()
mrst.Save , adPersistXML
End Sub

Private Sub Form_BeforeInsert(Cancel As Integer)
mrst.AddNew
mrst.Save , adPersistXML
mrst.MoveLast
End Sub

Private Sub Form_Close()
If Not mrst Is Nothing Then
If mrst.State <> adStateClosed Then
mrst.Close
End If
End If
End Sub

Private Sub Form_Load()
Set mrst = New ADODB.Recordset
With mrst
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockOptimistic
.Open CurrentProject.Path & "\test.xml", , , , adCmdFile
End With
Set Me.Recordset = mrst
End Sub
=================================================================

Now, I can everything (update,insert, delete).
If enybody now better solution for my idea, I heve "open mide" ;)

Best regards
Michal

P.S. sorry for my english ;)
 
Top