?
Solved

get a record and update that record

Posted on 2013-02-04
10
Medium Priority
?
372 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
[X]
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
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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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
 
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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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