-1

Does anyone know what this happens?

I have declared all my database objects as integers as well as my dataset objects as integer

when I run my ado query, I get values, from here, im trying to assign it as to a dataset, which is also declared as integers. But it keeps giving me

incompatible types: widestring and tintegerfield

Here is the exact code:

dxMemData1.FieldByName(dxMemData1RetailCalendarPeriodID) := 
  adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r;
Fly
  • 43
  • 1
  • 8
  • You need to add the **exact** code you are using which gives this error to your q. – MartynA Dec 15 '16 at 12:33
  • Here is the exact code dxMemData1.FieldByName(dxMemData1RetailCalendarPeriodID) := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsInteger; – Fly Dec 15 '16 at 12:40
  • I gave me information, I hope this is more clear – Fly Dec 15 '16 at 12:46

1 Answers1

7

dxMemData1.FieldByName(dxMemData1RetailCalendarPeriodID) := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r

should not compile.

It looks like dxMemData1RetailCalendarPeriodIDis a persistent field object you've created on you dxMemData1 dataset. The FieldByName method is used to find a field by its name, but you don't need to do that because you already have the field, dxMemData1RetailCalendarPeriodID!

So, what you need is simply

dxMemData1RetailCalendarPeriodID.AsInteger := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r

The reason for the compiler error is that FieldByName expects to be passed a string giving the name of a field whereas you were trying to pass the field itself, which is a TObject descendant not a string. The following would have worked, but is unnecessary because of the code I've already shown:

dxMemData1.FieldByName(dxMemData1RetailCalendarPeriodID.FieldName) := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r;

Update You say you are getting an "Invalid variant operation" error. Do you still get it if you use this code:

if not adoTreeWindow.FieldByName('RetailCalendarPeriodID').IsNull then    
  dxMemData1RetailCalendarPeriodID.AsInteger := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r

?

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Good, but it's more important to understand *why* it worked. – MartynA Dec 15 '16 at 12:58
  • 1
    Would the downvoter care to explain what is wrong factually/technically about this answer? – MartynA Dec 15 '16 at 12:59
  • I thought this works, but I get Invalid variant operation error – Fly Dec 15 '16 at 13:15
  • Use a local integer variable to receive the value `adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​r` and use this variable on the RHS of the assignment to `dxMemData1RetailCalendarPeriodID.AsInteger`. Then use the debugger to see if the error occurs on the first assignment. If it does, the reason is possibly that the field `adoTreeWindow.FieldByName('RetailCalendarPeriodID')` is empty (ie Null). – MartynA Dec 15 '16 at 13:24
  • adoTreeWindow.Close; adoTreeWindow.Open; dxMemData1.DisableControls; dxMemData1.open; dxMemData1.First; adoTreeWindow.First; while not(adoTreeWindow.eof) do begin for I := 0 to dxMemData1.FieldCount - 1 do begin dxMemData1RetailCalendarPeriodID.AsInteger := adoTreeWindow.FieldByName('RetailCalendarPeriodID').AsIntege‌​‌​r dxMemData1.Next; end; adoTreeWindow.Next; end; – Fly Dec 15 '16 at 13:31
  • This is exactly what I was doing. I checked what is returning and there are not empty fields or lines could it be the next for the memdata? because at that point it doesnt have a line to move to – Fly Dec 15 '16 at 13:35
  • Sorry, that code is full of errors. Firstly, the `for` loop is pointless because it just makes the same assignment over and over again (FieldCount - 1 times). Secondly, you should be putting the dxMemData1 dataset into `dsEdit` or `dsInsert` state (by calling .Edit or .Insert, depending on whether you are trying to edit an existing row in dxMemData1 or insert a new one) before trying to change its field(s). And you should be calling .Post afterwards. Thirdly, I'd be very surprised if dxMemData1.Next does what you maybe think it does. – MartynA Dec 15 '16 at 13:42
  • Finally, regarding your later comment, what I said is **not** "exactly what" you're doing, not in the code you've shown, anyway. – MartynA Dec 15 '16 at 13:43
  • Btw' I'm not going to comment further here about your "adoTreeWindow.Close; adoTreeWindow.Open; ..." code. If you want help with that, post it in a new q and make sure it is clear from your code what it is trying to do, including surrounding code context. – MartynA Dec 15 '16 at 13:47
  • 3
    Also btw, you should not have retracted your acceptance of my answer. It correctly answered the question you originally posted, and you have since tried to "move the goalposts" by raising a different point. As I've said, that point should be in a new q. – MartynA Dec 15 '16 at 14:35