get a record and update that record

Posted on 2013-02-04
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
Question by:sptech
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Expert Comment

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'
LVL 14

Expert Comment

by:Bill Ross
ID: 38850844

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.



Author Comment

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)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

by:Bill Ross
ID: 38851012

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 #



Author Comment

ID: 38851120
  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?
LVL 14

Expert Comment

by:Bill Ross
ID: 38851690

What is the error message?

What is the table structure of ctbal?

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



Author Comment

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

    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'

Author Comment

ID: 38854378
  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.
LVL 14

Accepted Solution

Bill Ross earned 500 total points
ID: 38855511

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.



Author Closing Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

717 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