0

I get Access violation error, but I know the code is correct, so where could be the problem? I'm trying to fill a ComboBox whit data from a local AccessDB.

var i : integer;
    x : string;
begin
      with DataModule3.ADOTable1 do begin
         if RecordCount > 0 then
           for i := 1 to RecordCount do begin
             RecNo := i;
             x := FieldByName('Teacher').AsString;
             ComboBox1.Items.Add(x);
           end;
       end;
end;

I have tried lots of things and nothing works, I have tried lots of combobox typed but still doesn't work the only time a combobx showed value was when I selected a row in table then it showed in combobox the rows value by which I need to filter...

Mr.Pengu
  • 107
  • 1
  • 2
  • 10
  • I don't know anything about the TADOTable, but should not the loop go from zero to RecordCount-1? – LU RD May 18 '15 at 08:10
  • @LURD, it internally sets the [`AbsolutePosition`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms676594%28v=vs.85%29.aspx) property which is 1 based index. – TLama May 18 '15 at 08:13
  • 2
    I would assume your problem is the use of Recno. I avoid it completely because it fails when you set a filter on your table and when I remember right it is not supported by all database. I use Table1.first; while not Table1.Eof do ...do your staff Table1.next – Christine Ross May 18 '15 at 08:14
  • @ChristineRoss I tried like you said, but it still drops the violation error, but it drops different violation address – Mr.Pengu May 18 '15 at 08:27
  • Is the DataModule3 created? Change the code to If Assigned(DataModule3) then with DataModule3.ADOTable1 do begin .... – RBA May 18 '15 at 08:35
  • Use debugger rather than trial error attempts (with debugger it's task for at most 10 seconds to find the root of your problem). What's worse, this sort of question is impossible to answer because we don't know what's happening with the objects you access (if they exist or not), nor your environment settings. Yes, we could give you perfectly safe code, but you would learn nothing then. – TLama May 18 '15 at 08:40
  • @TLama agree: give us exact line and error message – Christine Ross May 18 '15 at 08:48
  • @RBA thank you I guess it didn't create DataModule, and I guess if ADOTable has filtr on it doesn't give any values... – Mr.Pengu May 18 '15 at 08:58
  • Pity that you don't want to learn. I've had a finger on my mouse to post you a class helper for this task. Never mind, good luck with you guess work! ;-) – TLama May 18 '15 at 09:01
  • @TLama i want to learn, that's why i'm troubleshooting. But i have fixed it, now got problems whit filtering :D – Mr.Pengu May 18 '15 at 09:06
  • Allow me to recommend you learning how to debug a problem. Identify which reference to memory is invalid. I would also encourage you to stop using `with` for reasons that have been discussed many times. – David Heffernan May 18 '15 at 09:26

2 Answers2

1

Access Violation is raised most probably because you have forgot to instantiate your datamodule DataModule3. Verify this by calling Assigned function.

RBA
  • 12,337
  • 16
  • 79
  • 126
  • 1
    Most probably ? No way. Chances are the same for the `ADOTable1` and `ComboBox1` objects with the given information... – TLama May 18 '15 at 09:25
  • Most probably because I believe that OP does not know to create the components dynamically. Indeed, the chances are the same. – RBA May 18 '15 at 09:37
1
begin
    with DataModule3.ADOTable1 do 
        if Active then
            while not Eof do
                begin
                    ComboBox1.Items.Add(FieldByName('Teacher').AsString);
                    Next;
                end;
end;
Zam
  • 2,880
  • 1
  • 18
  • 33
  • 4
    Welcome to Stack Overflow. Standalone code without any explanation is no answer. You need to tell what's different about your code. In particular, how does your code solve the access violation? – Rob Kennedy May 18 '15 at 11:45