QueryDef Object ongeldig of niet meer ingesteld.

Status
Niet open voor verdere reacties.
Maakt niet uit. Ik puzzel wel even verder.

Bedankt voor je tijd en de moeite. Ik ben in ieder geval een stuk verder gekomen.
 
Laatst bewerkt:
Je Total waarde kun je ook veel simpeler uitereken met een Totalen query. Groeperen op DeckID en Sum op Qty. Heb je ook maar één tabel voor nodig. Simpel, en snel. En ook nog eens correct ;)
 
Gebruik ik daar DSum voor? Zo ja, wat kan ik als domein opgeven?
 
DSUm? Nee,gewoon de knop Totalen gebruiken. En groeperen op DeckID zoals ik al aangaf, en SOm gebruiken voor Qty.
 
Op dezelfde manier. Maak je totalen query, en kopieer de tekst. Hang 'm vervolgens in een string met de noodzakelijke aanpassingen.
 
Group By

YES!!!:cool: Het werkt nu. Ik heb het als volgt opgelost:
Code:
Option Compare Database
Option Explicit
Option Base 1

Private Type CardRow
    strName As String
    intQty As Integer
End Type

Private arrTypes As Variant
Public CardsArray(8) As CardRow

Public Sub CountCards(curRecord As Long)
    Dim MagicDb As DAO.Database
    Dim qStats As DAO.QueryDef
    Dim qResult As DAO.Recordset
    Dim Index As Integer
    Dim Counter As Integer

    Set MagicDb = CurrentDb
    
    'Fill Variables with names of cards you want to check.
    arrTypes = Array("Creature", "Sorcery", "Instant", "Enchantment", "Artifact", "Land", "Other", "Total")
    For Index = 1 To 8
        CardsArray(Index).strName = arrTypes(Index)
        CardsArray(Index).intQty = 0
    Next
    
    With MagicDb
    
        'Delete previous query if it exists
        If IsTableQuery("", "DeckStatsQuery") Then .QueryDefs.Delete "DeckStatsQuery"
        
        'Create a totalsquery of current record.
        Set qStats = .CreateQueryDef("DeckStatsQuery")
        qStats.SQL = "SELECT Cards.[Card Type].Value AS Category,  Sum(DeckItems.Qty) AS TotalCards " & _
                    "FROM Cards INNER JOIN DeckItems ON Cards.CardId = DeckItems.CardId " & _
                    "WHERE DeckItems.DeckId = " & curRecord & " " & _
                    "GROUP BY Cards.[Card Type].Value;"
        qStats.Close
        
        Set qResult = .OpenRecordset("DeckStatsQuery", dbOpenSnapshot)
        With qResult
        
            If .RecordCount > 0 Then   'Check wether the query is not empty.
                Do Until .EOF    'Loop through all records of the query.
                    For Index = 1 To 7   'Test the Card Type is one of the basic 6 types
                        If .Fields("Category").Value = CardsArray(Index).strName Then
                            CardsArray(Index).intQty = CardsArray(Index).intQty + .Fields("TotalCards")
                            Exit For
                        ElseIf Index = 7 Then
                            'Card Type is other than the 6 basic types
                            CardsArray(Index).intQty = CardsArray(Index).intQty + .Fields("TotalCards")
                        End If
                    Next
                    .MoveNext
                Loop
                'I use Dsum here, because sum gives the wrong total amount of cards
                'because of the multi value fields, wich counts double
                CardsArray(8).intQty = DSum("Qty", "DeckItems", "DeckId = " & curRecord)
            Else
                MsgBox "Table has no records", vbOKOnly, "Empty table"
            End If
        
        End With
        .QueryDefs.Delete qStats.Name
        .Close
        Set qResult = Nothing
        Set qStats = Nothing
        Set MagicDb = Nothing
    End With
End Sub

Bedankt iedereen en vooral Octafish. Ik weet niet of ik er anders uitgekomen was.
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan