|channel|>analysis<|message|>Need to output VB6 code generating SEPA XML Pain.008. Provide example string building.<|end|>
<|start|>assistant<|channel|>final<|message|>Below is a **complete, self‑contained VB 6 module** that builds an example SEPA “pain.008” (Credit Transfer Initiation) XML document and writes it to disk.
It shows the most common elements you’ll need:
* Message header (`GrpHdr`) – global information about the file
* Payment information block (`PmtInf`) – one or more payment groups
* Credit transfer transaction (`CdtTrfTxInf`) – each individual transfer
* Structured remittance information (`RmtInf/Strd`) – optional but useful
Feel free to copy‑paste the module into a new VB 6 project (or add it to an existing one).
After compiling and running, you’ll find a file named **`pain008_example.xml`** in the same folder as the executable.
---
## 1. The Code
```vb
' --------------------------------------------------------------------
' Module: SEPA_Pain008_Builder
' Purpose: Creates a simple pain.008 (SEPA Credit Transfer Initiation)
' XML document and writes it to disk.
'
' Author : ChatGPT / OpenAI
' Date : 2025‑08‑22
' --------------------------------------------------------------------
Option Explicit
' ------------------------------------------------------------
' Public entry point – call this from Form_Load or any button
' ------------------------------------------------------------
Public Sub BuildAndSavePain008()
Dim xmlString As String
' 1. Build the XML as a string
xmlString = CreatePain008XML()
' 2. Write it to disk (current folder)
Dim filePath As String
filePath = App.Path & "\pain008_example.xml"
If SaveTextToFile(filePath, xmlString) Then
MsgBox "SEPA pain.008 file created:" & vbCrLf & filePath, vbInformation
Else
MsgBox "Failed to write SEPA file.", vbCritical
End If
End Sub
' --------------------------------------------------------------------
' CreatePain008XML – builds the XML document in a single string.
' --------------------------------------------------------------------
Private Function CreatePain008XML() As String
Dim s As String
' 1. XML header + root element with namespaces (taken from pain.008 schema)
s = ""
s = s & vbCrLf & _
""
' 2. Group Header (GrpHdr)
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " MSG-001" ' unique message id
s = s & vbCrLf & " " & Format(Now, "yyyy-MM-ddTHH:mm:ss") & ""
s = s & vbCrLf & " 2" ' number of transactions
s = s & vbCrLf & " 2500.00" ' total amount (sum of all)
' -- Initiating Party (who sends the file) --
s = s & vbCrLf & " "
s = s & vbCrLf & " Acme Corp Ltd."
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " 123456789"
s = s & vbCrLf & " SEPA"
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
' 3. Payment Information Block – one block for all payments (you can add more)
s = s & vbCrLf & " "
s = s & vbCrLf & " PMT-001"
s = s & vbCrLf & " TRF" ' Transfer
s = s & vbCrLf & " false"
s = s & vbCrLf & " 2"
s = s & vbCrLf & " 2500.00"
' -- Payment Method details (SEPA Credit Transfer) --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " SEPA"
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " CORE"
s = s & vbCrLf & " "
s = s & vbCrLf & " FRST" ' First transfer
s = s & vbCrLf & " "
' -- Creditor Agent (the bank that will receive the payment) --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " DEUTDEFFXXX" ' Example BIC
s = s & vbCrLf & " "
s = s & vbCrLf & " "
' -- Creditor (the beneficiary) --
s = s & vbCrLf & " "
s = s & vbCrLf & " John Doe"
s = s & vbCrLf & " "
' -- Creditor Account (IBAN) --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " DE89370400440532013000"
s = s & vbCrLf & " "
s = s & vbCrLf & " "
' -- Payment Identification (Reference) --
s = s & vbCrLf & " "
s = s & vbCrLf & " INSTR-001"
s = s & vbCrLf & " E2E-1234567890"
s = s & vbCrLf & " "
' -- Credit Transfer Transaction – first payment --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " 1500.00"
s = s & vbCrLf & " "
' -- Structured Remittance Information (optional but recommended) --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " INV"
s = vBCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " [INV-2025-001]"
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
' -- Credit Transfer Transaction – second payment --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " 1000.00"
s = s & vbCrLf & " "
' -- Structured Remittance Information (optional) --
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " INV"
s = vBCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " [INV-2025-002]"
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
s = s & vbCrLf & " "
' Close Payment Information Block
s = s & vbCrLf & " "
' Close Customer Credit Transfer Initiation and root Document element
s = s & vbCrLf & " "
s = s & vbCrLf & ""
CreatePain008XML = s
End Function
' --------------------------------------------------------------------
' SaveTextToFile – helper to write a string into a file.
' Returns True on success, False otherwise.
' --------------------------------------------------------------------
Private Function SaveTextToFile(ByVal FileName As String, ByVal TextData As String) As Boolean
On Error GoTo ErrHandler
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim ts As Object
Set ts = fso.CreateTextFile(FileName, True, False) ' overwrite, ASCII
ts.Write TextData
ts.Close
SaveTextToFile = True
Exit Function
ErrHandler:
SaveTextToFile = False
End Function
```
---
## 2. How to Use It
1. **Create a new VB 6 project** (Standard EXE or ActiveX DLL).
2. Add a **module** (`Insert → Module`) and paste the code above.
3. In `Form_Load` (or any button click event), call:
```vb
Private Sub Form_Load()
BuildAndSavePain008
End Sub
```
4. Compile & run – you’ll get `pain008_example.xml`.
---
## 3. What the XML Looks Like
Here’s a **compact preview** of what the file contains (formatted for readability):
```xml
MSG-001
2025-08-22T12:34:56
2
2500.00
Acme Corp Ltd.
123456789
SEPA
PMT-001
TRF
false
2
2500.00
SEPA
CORE
FRST
DEUTDEFFXXX
John Doe
DE89370400440532013000
INSTR-001E2E-1234567890
1500.00
INV
[INV-2025-001]
1000.00
INV
[INV-2025-002]
```
---
## 4. Customising the Example
| Section | What to change |
|---------|----------------|
| `MsgId`, `PmtInfId` | Use a unique identifier (e.g., GUID or timestamp). |
| Dates (`CreDtTm`) | Format as `yyyy-MM-ddTHH:mm:ss`. |
| Amounts & `CtrlSum` | Sum the transaction amounts; keep two decimal places. |
| BIC / IBAN | Replace with your own bank and beneficiary details. |
| Reference numbers (`EndToEndId`, `InstrId`, remittance refs) | Use meaningful values per your business logic. |
---
## 5. Validation
After generating the file, validate it against the **pain.008.001.02** XSD:
```bash
xmllint --noout --schema pain.008.001.02.xsd pain008_example.xml
```
If you have no XML validator, many banks provide online SEPA XML validators that will accept this sample.
---
### Happy coding!
Feel free to extend the module with loops (to generate many transactions) or a UI for entering data.