私はVBAを初めて使っていますが、うまくいけばいいと思います。VBAのループを使ってproduct1、product2 ...などの値を取得
私は異なるクライアントを持つテーブルを持っており、すべてのクライアントはそれ自身のIDを持っています。
この表では、すべてのクライアントが異なる数の製品を購入しています。 たとえば、ID = 3のクライアントは3つの製品を購入します。 表では、product1、product2、product3という名前を付けます。
Dim intTotal, totalCompra As Double
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim rs As DAO.Recordset
Dim cantidad As Integer
Dim intCantidad As Integer
Dim intPrecio As Double
Set wApp = New Word.Application
Set wDoc = wApp.Documents.Open("C:\...\factura_alcazaba.docx")
Set rs = CurrentDb.OpenRecordset("Select * FROM Ventas_Alcazaba WHERE ID =" & idInt & ";")
wDoc.Bookmarks("Name").Range.Text = Nz(rs!Nombre_cliente, "")
wDoc.Bookmarks("NIF").Range.Text = Nz(rs!NIF, "")
wDoc.Bookmarks("Fecha").Range.Text = Nz(rs!Fecha, "")
cantidad = Nz(rs!Numero_productos, "")
If Not IsEmpty(rs!Cantidad_prod1) Then
intCantidad = rs!Cantidad_prod1
wDoc.Bookmarks("cantidad1").Range.Text = Nz(rs!Cantidad_prod1, "")
wDoc.Bookmarks("descripcion1").Range.Text = Nz(rs!Descripcion_prod1, "")
intPrecio = rs!Precio_prod1
intTotal = intPrecio * intCantidad
totalCompra = totalCompra + intTotal
wDoc.Bookmarks("precio1").Range.Text = Nz(intPrecio, "")
wDoc.Bookmarks("prod1_total").Range.Text = Nz(intTotal, "")
End If
'For i = 1 To cantidad
'intCantidad = rs!Cantidad_prod + i
'wDoc.Bookmarks("cantidad" + i).Range.Text = Nz(rs!["Cantidad_prod" & i], "")
' wDoc.Bookmarks("descripcion" & i).Range.Text = Nz(rs!["Descripcion_prod" + i], "")
' intPrecio = Nz(rs!["Precio_prod" & i], "")
'intTotal = intPrecio * intCantidad
' totalCompra = totalCompra + intTotal
' wDoc.Bookmarks("precio1").Range.Text = Nz(intPrecio, "")
' wDoc.Bookmarks("prod1_total").Range.Text = Nz(intTotal, "")
'Next i
wDoc.SaveAs2 "C:\Factura_" & rs!ID & ".docx"
wDoc.Close False
wApp.Quit
もし私がテーブルに存在するならば、私はすべての値を得ることができます。しかし、それは非常に非効率的で、私はループでそれをやりたいと思います。 しかし、私は異なる製品を呼び出す方法を知りません最終番号を変更します。
Uhmmm、私が知っているのは、私が何かを簡単にしたいからです。私の兄弟にとっては好意的です。 私の兄は売り手ではないので、最大10個の商品を追加します。彼は別のビジネスのためだけに請求書を作成するので、彼がもっと多くの製品を必要とする場合は、自分で追加することができます。 私はこれを作るのが普通です。 – AntoIba
ええ、それは事です:あなたはそれを単純にしないでください、あなたはそれをより困難にします。そして彼。 Clients - Products - Orders - Order Positionsのテーブル構造は、Northwindサンプルデータベース(私は思う)を含むほぼすべてのデータベースの例にあります。それは初めてです。 – Andre
はい、私は知っています。問題は、私がアクセスがとても素敵だったことを知らなかったということでした。私は、テーブル、製品、クライアント、売上とキーIDを持つ関連テーブルを作成するつもりです。もう少し時間がかかるでしょうが、それはより良いでしょう。 – AntoIba