Refresh DBリンク

Sub OO4O()
	'ODBCリンクを新アカウントでリフレッシュする
	Private Sub btnGO_Click()

	Dim wkTBL As String, wkLEN As String, wkDSN As String
	Dim WKSRCTBL As String, wkUID
	Dim MyTbl As DAO.TableDef
	Dim prp As DAO.Property

	If IsNull(Me.txtID.Value) Or IsNull(Me.txtPASS.Value) Then
		MsgBox “ID/パスワードをセットしてください"
		Exit Sub
	End If

	For Each MyTbl In CurrentDb.TableDefs
		wkTBL = MyTbl.Name
		WKSRCTBL = MyTbl.SourceTableName
		'ODBC link?
		If MyTbl.Attributes And dbAttachedODBC Then
			'DSN CHECK
			wkLEN = InStr(1, MyTbl.Connect, “DSN=") + 4
			wkDSN = Mid(MyTbl.Connect, wkLEN, InStr(wkLEN, MyTbl.Connect, “;") – wkLEN)
			'USER CHECK
			wkLEN = InStr(1, MyTbl.Connect, “UID=") + 4
			wkUID = UCase(Mid(MyTbl.Connect, wkLEN, InStr(wkLEN, MyTbl.Connect, “;") – wkLEN))

			'DSN MATCH and USER ID match?
			If wkDSN = txtDSN.Value And ((wkUID = UCase(Me.txtID.Value) And _
				chkUID.Value = True) Or chkUID.Value = False) Then
				'SUB link
				AUTO_LINK wkTBL, WKSRCTBL, wkDSN, Me.txtID.Value, Me.txtPASS.Value, Me.chkPW
			End If
		End If
	Next
	'
	MsgBox “done!"
End Sub

Sub AUTO_LINK(dbtable As String, dbname As String, _
	dsn As String, uid As String, pwd As String, flgpw As Boolean)
	'current table name, source table name, DSN,UID,PWD,SAVE password
	Dim db As Database, TblDef As TableDef
	Dim prp As DAO.Property

	On Error Resume Next
	'Delete current link
	DoCmd.DeleteObject acTable, dbtable
	On Error GoTo 0

	Set db = DBEngine.Workspaces(0).Databases(0)

	Set TblDef = db.CreateTableDef(dbtable)
	TblDef.Connect = “ODBC;DSN=" & dsn & “;UID=" & uid & “;PWD=" & pwd
	TblDef.SourceTableName = dbname
	'include password
	If flgpw Then TblDef.Attributes = dbAttachSavePWD
	'make link
	db.TableDefs.Append TblDef
	'set link account in memo field
	Set prp = TblDef.CreateProperty(“Description", dbText, dsn & “:" & uid)
	TblDef.Properties.Append prp
	TblDef.Properties.Refresh

	'refresh table list
	TblDef.RefreshLink

End Sub

VBA:XML作成

'MDRを読み込んで新規XMLファイルを作成するVBAプログラム
Sub export_xml()

	Dim xdoc As New DOMDocument, xtree As IXMLDOMElement
	Dim PI As IXMLDOMProcessingInstruction
	Dim LOB As IXMLDOMElement, FAM As IXMLDOMElement
	Dim BRAND As IXMLDOMElement
	Dim RS As Recordset
	
	'xml header
	Set PI = xdoc.createProcessingInstruction(“xml", “version='1.0′")
	Call xdoc.appendChild(PI)
	'root tree
	Set xtree = xdoc.createElement(“PRODUCT")
	Call xdoc.appendChild(xtree)
	'get data from database
	Set RS = CurrentDb.OpenRecordset(“select * from xxxx")
	
	Do While Not RS.EOF
		'親
		Set LOB = xdoc.createElement(“LOB")
		Call LOB.setAttribute(“NAME", RS!PRODUCT)
			'子1
			Set FAM = xdoc.createElement(“FAMILY")
			FAM.Text = RS!FAMILY
			Call FAM.setAttribute(“CODE", RS!FAMILY_ID)
			Call LOB.appendChild(FAM)
			'子2
			Set BRAND = xdoc.createElement(“BRAND")
			BRAND.Text = RS!BRAND
			Call BRAND.setAttribute(“CODE", RS!BRAND_CD)
			Call LOB.appendChild(BRAND)
		'親書き出し
		Call xtree.appendChild(LOB)
		'next record
		RS.MoveNext
	Loop
	'db close
	RS.Close
	'xml保存
	xdoc.Save (“c:\temp\test.xml")

End Sub