2017-05-05 7 views
0

결과를 표시하는 입력 필드, 버튼 및 영역이있는 기본 검색 양식을 만들었습니다.액세스 : 양식 결과를 표로 변환

입력란에 키워드가 입력되고 "검색"버튼을 클릭하면 결과가 표시됩니다.

Private Sub mySearchQuery_Click() 
Dim strSelect As String 
Dim strWhere As String 

If Len(Trim(Me!searchFirst.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND First_Name Like ""*" & Me!searchFirst.Value & "*""" 
End If 
If Len(Trim(Me!searchLast.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND Last_Name Like ""*" & Me!searchLast.Value & "*""" 
End If 
If Len(Trim(Me!searchState.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND State Like ""*" & Me!searchState.Value & "*""" 
End If 
If Len(Trim(Me!searchLIC.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND LIC Like ""*" & Me!searchLIC.Value & "*""" 
End If 
If Len(Trim(Me!searchNPN.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND NPN Like ""*" & Me!searchNPN.Value & "*""" 
End If 
If Len(Trim(Me!searchEmail.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND Email Like ""*" & Me!searchEmail.Value & "*""" 
End If 
If Len(Trim(Me!searchDateFrom.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND EXP >= #" & Me.searchDateFrom.Value & "#" 
End If 
If Len(Trim(Me!searchDateTo.Value) & vbNullString) > 0 Then 
    strWhere = strWhere & " AND EXP <= #" & Me.searchDateTo.Value & "#" 
End If 

strSelect = "SELECT * FROM MasterData" 
If Len(strWhere) > 0 Then 
strSelect = strSelect & " WHERE " & Mid(strWhere, 6) 
End If 

Me.RecordSource = strSelect 
' SubForm.Form.RecordSource = sql 
End Sub 

이 검색 버튼을 주어진 기준에 따라 항목을 검색 할 것입니다 :

검색 버튼은 다음과 같은 코드가 있습니다. State 입력란에 "NY"를 입력하고 Search 버튼을 누르면 State가 NY로 설정된 결과를 보여줍니다.

제 질문은 : 어떻게이 반환 된 결과를 양식에서 테이블로 내보내고 저장합니까? 내가 그렇게 할 수 있다면 좋을거야.

답변

0

간단한 SQL 만 사용하십시오. 그리 어렵지 않습니다.

DoCmd.RunSQL "SELECT * INTO MyTable FROM MasterData" & strWhere 
+0

답장을 보내 주셔서 감사합니다. 이 버튼을 만들 수 있습니까? 또는이 코드를 어디에 넣을 수 있는지 말해 줄 수 있습니까? – ChrisP777

+0

버튼을 만들 수 있지만 검색 기능 (모듈 또는 템플리트의 선언 섹션)이 아닌 곳에서 strWhere를 저장해야합니다. 또는 검색 기능에 옵션으로 추가 할 수 있습니다. –