Predmet:Re: Query od vise tabela
Evo moja pretraga.
Napravi Novu formu i na nju stavi 2 list boxa.
Prvi list neka se zove 'List0' a drugi neka sezove 'temp'.
U list0:
rowsource type neka bude value lista.
column count 2
column with 3;0
u list temp:
column count 28 ili manje po zelji
Napravi i jedno tekstualno polje koje ce se zvati 'uslov'.
I jos jedan komandni taster 'command2'
Evo kod:
PreuzmiIzvorni kôd (Visual Basic):Private Sub Command2_Click()
Dim uslov, Imepolja As String
Dim tip As Integer
Dim SQl As String, SQlUslov As String
Dim Itm, Ctl As Control
uslov = Me.uslov
If Format$(uslov) = "" Then GoTo Kraj
SQl = "SELECT * FROM TblTemp "
Set Ctl = Me.List0
For Each Itm In Ctl.ItemsSelected
Imepolja = Trim(Ctl.Column(0, Itm))
tip = Trim(Ctl.Column(1, Itm))
DodajUslov Imepolja, tip, uslov, SQlUslov
Next Itm
If SQlUslov <> "" Then
SQl = SQl & " WHERE " & SQlUslov
Else
SQl = "SELECT * FROM tblTemp WHERE False<>False"
End If
Me.temp.RowSource = SQl
Kraj:
End Sub
Private Sub Form_Load()
Dim Db As DAO.Database
Dim tdf As DAO.TableDef
Dim Fld As DAO.Field
Dim tmp As String
Set Db = CurrentDb
Set tdf = Db.TableDefs("tblTemp")
For Each Fld In tdf.Fields
tmp = tmp & Fld.Name & ";" & Fld.Type & ";"
Next
tmp = Left(tmp, Len(tmp) - 1)
Me.List0.RowSourceType = "Value List"
Me.List0.RowSource = tmp
End Sub
Sub DodajUslov(Imepolja As String, tip As Integer, uslov, SQlUslov As String)
Dim Poz As Integer
If SQlUslov <> "" Then
SQlUslov = SQlUslov & " OR "
End If
Select Case tip
Case 1
If uslov = Val(-1) Or uslov = Val(0) Then
SQlUslov = SQlUslov & Imepolja & "=" & uslov
Else
If SQlUslov <> "" Then
SQlUslov = Left(SQlUslov, Len(SQlUslov) - 4)
End If
End If
Case 4
If IsNumeric(uslov) Then
SQlUslov = SQlUslov & Imepolja & "=" & uslov
Else
If SQlUslov <> "" Then
SQlUslov = Left(SQlUslov, Len(SQlUslov) - 4)
End If
End If
Case 8
Poz = InStr(1, uslov, "-")
If Poz > 0 Then
On Error GoTo PRAZNO
SQlUslov = SQlUslov & "BETWEEN #" & Format(Left(uslov, Poz - 1), "dd-mm-yyyy") _
& "# AND #" & Format(Mid(uslov, Poz + 1), "dd-mm-yyyy") & "#"
PRAZNO:
If SQlUslov <> "" Then
SQlUslov = Left(SQlUslov, Len(SQlUslov) - 4)
End If
Err.Clear
On Error GoTo 0
Else
Dim Dat As Date
On Error Resume Next
Dat = uslov
If Err.Number > 0 Then
If SQlUslov <> "" Then
SQlUslov = Left(SQlUslov, Len(SQlUslov) - 4)
End If
Err.Clear
On Error GoTo 0
Else
uslov = "#" & Format(uslov, "dd-mm-yyyy") & "#"
SQlUslov = SQlUslov & Imepolja & "=" & uslov
End If
End If
Case Is > 9
SQlUslov = SQlUslov & Imepolja & " Like '*" & uslov & "*'"
End Select
End Sub
Trebalo bi malo istestirati.
Mozes dodati i u list0 neke druge nazive polja koji ce vidjeti korisnik.
Podrska samo putem foruma, jer samo tako i ostali imaju koristi od toga.