Delphi的代码小问题——初学

来源:百度知道 编辑:UC知道 时间:2024/06/30 14:44:58
源代码如下:
procedure TfModiLeave.FillCombo(cboX: TCustomComboBox;
sTable, sField: String);
var sSQL: String;
begin
sSQL := 'SELECT DISTINCT ' + sField + ' FROM ' + sTable
+ ' WHERE ' + sField + ' IS NOT NULL AND '
+ sField + '<>''''';
with DDMain.ADOD_Cbo do
begin
Close;
CommandText := sSQL;
Open;
while not Eof do
begin
cboX.Items.Add(FieldByName(sField).AsString);
Next
end;
Close;
end;
end;
代码的目的是能用不重复的字段值填充组合框,事先在私有段声明了procedure FillCombo(cboX: TCustomComboBox; sTable, sField: String);
后又在窗体创建部分写了
FillCombo(DBcomboBox3, "离退休员工基本信息","职务");
。。。。。。
出现的错误:1在输入文件中的非法字符“”
2在输入文件中的非法字符“基”
3在输入文件中的非法字符“职”
4未说明的标识符“CommandText”???望高手指点哪里错了?

1.在delphi中是不使用双引号的,字符串常量和单字符用的都是单引号,你把双引号改为单引号就行了;
问题2,3均因问题1所导致;
4.不确定你FillCombo函数体中 ADOD_Cbo是哪种ADO控件,确认是否为ADODataSet,其实用ADOConnection+ADOQuery比较好,常规用法

设好:ADOQuery1ovrn数据库连接属性,添加ComboBox1

with ADOQuery1 do
try

Close;
SQL.Clear;
SQL.Add('select DISTINCT 职务 from 离退休员工基本信息 where 职务 is not null');
Prepared;
open;

ComboBox1.Items.Clear;

while not Eof do
begin
ComboBox1.Items.Add(Fieldbyname('职务').AsString);
Next;
end;

Close;

except
MessageBox(GetActiveWindow(), '操作数据库失败!', '提示!', mb_iconwarning);
exit;
end;

在语句中使用变量,要加上这个函数:pchar(a).a为你的变量.你试下中不中!