Jump to content

Recommended Posts

Posted

Boa tarde,

Será que alguém me poderá ajudar?

Estou criando uma base de dados em access, que permita importar um segmento de texto (string) de um ficheiro .txt e adicionar a tabela no Access.

Exemplo do conteúdo de uma ficheiro (SALES100214.txt)recebido em "C:\FilesVendasDiarios\*.txt":

BFH0000000101790331 203PROD1402110103 000001

BCH00000002020221140210F

BOH000000030322990833140210 USD2

BKT0000000406 000001 NR019X 331

BKS00000005241402090000013312400747471 37906004134925496FFVV 22990833

BKS00000006301402090000013312400747471 30000004990{0000004990{ YC 0000000055{US

BKS00000010301402090000013312400747471 30000000000{0000004990{ XF

BKS00000011301402090000013312400747471 30000000000{0000004990{ YR 0000000202I

BKS00000012391402090000013312400747471 3I 000000000000000{

BKS00000013461402090000013312400747471 3 PENALTIES MAY APPLY -BG S4

BAR00000018661402090000013312400747471 31WBVI063606160000939603244

BKP0000002184140209000001MSCC 0000006117DWBVI063606160000939

BKP0000002284140209000001CA 0000000000{

BKT0000002306 000002 NR019X 331

BKS00000024241402090000023312400747473 57906004134925522FFVV 22990833

BKS00000031391402090000023312400747473 5I 000000000000000{

BKS00000032461402090000023312400747473 5 PENALTIES MAY APPLY -BG:S4

BAR00000037661402090000023312400747473 51WBVI063606160000939603244

BKF00000038811402090000023312400747473 5 1BOS S4 PDL208.00S4 BOS291.00NUC499.00END

BKF00000039811402090000023312400747473 5 25.00XA7.00XY2.50AY21.17YP11.78PT4.50XF20.29YRXF BOS4.5

BKP0000004084140209000002MSCC 0000006117DWBVI063606160000939

BKP0000004184140209000002CA 0000000000{

BKT0000004206 000003 NR019X 331

BKS00000043241402090000033312400747472 47906004134925533FFVV

BKS00000044301402090000033312400747472 40000004990{0000004990{

BKS00000045301402090000033312400747472 40000000000{0000004990{

Este ficheiro, não tem colunas. Gostaria de importar para uma tabela em access todos os números Ticket´s (assinalado a vermelho), a primeira linha do ficheiro BKS (assinalado a azul) que está a seguir da linha BKT (assinalado a verde).

A tabela criada em access teria basicamente só um campo que seria o número de documento.

Tenho uma tabela criada em access (Tabela_TicketInFile) com os campos “ID.Ticket” e “TicketInFileNumber”. A tabela não poderá ter registos com número Ticket´s em duplicado.

Private Sub ImportTicket_Click()

Dim linha As String
Dim db As DAO.Database, rs As DAO.Recordset
Dim AbrirFicheiro As New CommonDialog
Dim Caminho As String

Caminho = AbrirFicheiro.GetOpenFile(Me.Hwnd, "Selecione o ficheiro a ser importado", "C:\Users\20000252\Desktop")
If Len(Caminho) > 0 Then
txt_Arquivo = Caminho
Else
txt_Arquivo = vbNullString
End If
Open ("C:\Users\20000252\Desktop\EMD140211.txt") For Input As #1

Set db = DBEngine.Workspaces(0).Databases(0)
Set rs = db.OpenRecordset("Tabela_TicketInFile", dbOpenTable)
Do While Not EOF(1)
Line Input #1, linha
If Left$(linha, 1) = "BKS" Then
With rs
.AddNew
!TicketInFileNumber = Mid(linha, 25, 10)
.Update
End With
End If
Loop
MsgBox "Importação Concluída com Sucesso!!"
rs.Close
db.Close
Close #1
End Sub

Os exemplos que tenho encontrado na net são para a importação de um ficheiro .txt com colunas e não por linhas.

Agradeço deste já a quem me possa ajudar.

Carlos Machado

Posted

Algumas correcções:


Private Sub ImportTicket_Click()

Dim linha As String
Dim db As DAO.Database, rs As DAO.Recordset

'Dim AbrirFicheiro As New CommonDialog
'é preferível usar o objecto do Office.  Ver Função  AbrirFicheiro()
Dim txt_Arquivo As String

txt_Arquivo = AbrirFicheiro
If txt_Arquivo = "" Or Dir(txt_Arquivo) = "" Then Exit Sub

Open txt_Arquivo For Input As #1

'Set db = DBEngine.Workspaces(0).Databases(0)
'Set rs = db.OpenRecordset("Tabela_TicketInFile", dbOpenTable)
Set rs = CurrentDb.OpenRecordset("Tabela_TicketInFile") 'é mais simples

Do Until EOF(1)
	Line Input #1, linha
	If Left$(linha, 3) = "BKT" Then ' verde BKT
		'Falta ler a linha seguinte
		Line Input #1, linha
		rs.AddNew
		rs!TicketInFileNumber = Mid(linha, 26, 13)
		rs.Update
	End If
Loop
MsgBox "Importação Concluída com Sucesso!!"
rs.Close
'db.Close
Close #1
End Sub

Private Function AbrirFicheiro() As String
'Referenciar o Microsoft Office xx.x Object Library
Dim dlg As FileDialog

Set dlg = Application.FileDialog(msoFileDialogFilePicker)
dlg.Filters.Clear
dlg.Filters.Add "Ficheiros de Texto", "*.txt"
dlg.FilterIndex = 0
'dlg.InitialFileName = PathInicial
dlg.AllowMultiSelect = False
If dlg.Show = True Then AbrirFicheiro = dlg.SelectedItems(1)
End Function

O caminho mais curto para conseguir fazer muitas coisas é fazer uma de cada vez. Samuel Smiles

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.