How to store SQL Server table names in a combobox in vfp?
After I read on VFP ComboBox Content from SQL Server 2005 Express table
Can someone help me to store table names from SQL Server to a combobox in VFP?
I tried with sqltables()
and store in Grid1 vfp that can show name table is.
My code on load form:
public KONEKSI, mskedul1
STORE SQLCONNECT('surya','surya','surya_database') TO KONEKSI
IF KONEKSI > 0
SQLEXEC(KONEKSI,'use ksp_arthasurya')
endif
SQLTables(m.KONEKSI,'TABLE','tableList')
My code on init form:
thisform.grid1.recordsource = 'tablelist'
and on my grid1 shown table that contain on my SQL Server dbo.ksp_arthasurya
My question is: I want the names of all tables that belong to the dbo.ksp_arthasurya
schema shown in my combobox in VFP. If I click on a name in the VFP combobox, my grid1 in VFP should show the data in that table.
albharons is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
(You are asking the same question on multiple sites, aren’t you reading the replies? Anyway)
Here is a sample form that would get the table names into a combobox and upon selection of a table, show that table’s top 100 rows in a grid:
(Note that since a combobox with style 2 is something that user cannot type, this code wouldn’t be treated it as prone to SQL injection attack – but in your real use case you should thoroughly make that assessment)
Public oForm
oForm = Createobject('SampleForm')
oForm.Show()
Define Class SampleForm As Form
Height=800
Width =600
Add Object cmbTables As ComboBox With Left=10, Top=10, Width=300,Style=2
Add Object grdViewer As Grid With Left=0,Top=40,Height=760,Width=600,Anchor=15
Procedure Init
Local KONEKSI, lcSQL
STORE SQLCONNECT('surya','surya','surya_database') TO KONEKSI
* Store Sqlstringconnect('Driver={SQL Server Native Client 11.0};Server=...;Database=ksp_arthasurya;...') To m.KONEKSI
IF m.KONEKSI < 0
MESSAGEBOX("Couldn't connect.",0,'SQL Server connection',5000)
RETURN .F.
endif
SQLEXEC(m.KONEKSI,'use ksp_arthasurya')
This.AddProperty('nHandle', m.KONEKSI)
TEXT TO m.lcSQL noshow
Select CAST(QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(Name) as varchar(200)) AS TableName
FROM sys.Tables
WHERE is_ms_shipped=0
ORDER by schema_name(schema_id), Name
ENDTEXT
lnResult = SQLExec(m.KONEKSI, m.lcSQL,'TableList')
If m.lnResult < 0
Aerror(aWhy)
Messagebox(aWhy[2])
SQLDisconnect(0)
Return .F.
Else
With Thisform.cmbTables
.RowSourceType = 3
.RowSource = 'select TableName from TableList into cursor crsTables nofilter'
.ListIndex=0
Endwith
Endif
Endproc
Procedure cmbTables.InteractiveChange
Local lcSQL
Use In (Select('crsResult'))
TEXT TO m.lcSQL TEXTMERGE noshow
select top(100) * from << TRIM(crsTables.TableName) >>
ENDTEXT
SQLExec(Thisform.nHandle, m.lcSQL ,'crsResult')
With Thisform.grdViewer
.ColumnCount = -1
.RecordSource = "crsResult"
.AutoFit()
Endwith
ENDPROC
PROCEDURE Destroy
SQLDISCONNECT(this.nHandle)
endproc
Enddefine