Solved

get a record and update that record

Posted on 2013-02-04
10
329 Views
Last Modified: 2013-02-05
I am having a serious mental block.  I need to retrieve a record from my DB based on a query

dim rs as adodb.recordset
dim sSQL as string

set rs = new.adodb.recorset
sSQL = "Select balance from ctbal" & " where ctbalID = " & Me.txtID

when I debug print this I get:

Select balance from ctbal where ctbalID = 1

if the field in question "Balance" has a value say 2 then why does my code say "empty" when I place my cursor over the field.

Here is exactly what I am trying do accomplish:

1) Extract the field value of "balance" from my table CTbal.
2) Add to the field from my form to create a "new" balance
 3) Update my table to reflect the "new" balance
0
Comment
Question by:sptech
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:esolve
ID: 38850821
What is the datatype of the ctbalID column? If it is not numeric then your query should look like this:

sSQL = "Select balance from ctbal " & " where ctbalID = '" & Me.txtID & "'"

Select balance from ctbal where ctbalID = '1'
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 38850844
Hi,

Seems like one step to me.

Sim strSQL as string
strSql = "UPDATE ctbal SET balance = (balance + " & me!.fieldfrommyform & ") where ctbalID = '" & Me!txtID & "'"
currentdb.execute strSql

No need to create a recordset - just do the update.

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38850909
the field inside the table "balance is a HH:NN (short time) field as is the field on my form.  The balance field inside my table is 01:00.  The me.fieldname is 10:25 (also short time)

when I try the Balance+me.fieldname I get (10:2501:00)
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 38851012
Hi,

If you are trying to add time then you must add correct delimiter.  Try this:

strSql = "UPDATE ctbal SET balance = (balance + #" & me!fieldname & "#) where ctbalID = '" & Me!txtID & "'"
 
Date/time requires #

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38851120
Bill,
  I got the time issue resolved (it isn't as clean as yours but it works)(I will try your solution in the morning).  

I am getting an error when I try to execute the strsql string you created for me.  I think it has to do with currentdb.execute, strsql.  What should I have done initially to get the db ready to update?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 14

Expert Comment

by:Bill Ross
ID: 38851690
Hi,

What is the error message?

What is the table structure of ctbal?

currentdb.execute does not need any prep - it just runs.

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38853794
I am getting "argument not optional"  the field is a date/time field with short time. when I hover over the me.fieldname instead of seeing 10:25 (short time) I see 10:25:00 am. I am using ADO and not DAO can this be part of the issue?
I have the sub listed below:

Private Sub CTBal()
Dim sSQL As String

On Error GoTo err_label

sSQL = "update ctbal set balance = (#" & balance & "# + #" & Me!txtTCTAppr & "# ) where ID =  ' " & Me!txtID & " ' "
Debug.Print sSQL
CurrentDb.Execute , sSQL


Exit Sub
   

err_label:
    MsgBox "Error creating new record" & vbNewLine & Err.Description, vbCritical, "Error creating new record"

End Sub

the results of the debug.print:

update ctbal set balance = (#1:00:00 AM# + #10:25:00 AM# ) where ID = '1'
0
 

Author Comment

by:sptech
ID: 38854378
Bill
  A little clarification on what I am trying to accomplish.

My users travel while traveling they earn comp time vice over time.  I am trying to keep track of how much they have earned, used and the balance.  I can track how much earned, how much used, but the balance issue is kicking my behind.  In my earlier comment I have an existing balance of 1 hour, my user earned 10 hours 25 minutes for a grand total of 11 and 25 minutes.  "Balance" shows when I hover the cursor to be 11 hours and 25 minutes, but the currentdb.execute, ssql is throwing the error "argument not optional"

to add a monkey wrench I will have to eventually add logic to round to the nearest quarter hour.
0
 
LVL 14

Accepted Solution

by:
Bill Ross earned 500 total points
ID: 38855511
Hi,

I don't think it's a good idea to keep results of calculations in a table.  If you know how much they have earned (tblEarnedTime) and how much they have used (tblTimeUsed) ou can always get the balance.  No need to try to update the balance as you go.  Look at your checkbook as an example.

A query is an easy tool to use for the answer and subtract the total of one from another.

The expression below will give you decimal hours rounded to the nearest .25 hours between 2 time fields (me!fldStop and me!fldStart) and set the fldNearestQtrHour field.

me!fldNearestQtrHour=round(((me!fldStop-me!fldStart1) * 60 * 24) / 60 /.25,0) * .25

Put this code in the after update event of your form to save the hours earned and hours used.  Use a query anytime the balance is needed.

Regards,

Bill
0
 

Author Closing Comment

by:sptech
ID: 38858201
Bill gave me answers to two issues in this question. I greatly appreciate his helping a newbie such as myself.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now